functions.php 132 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145
  1. <?php
  2. if (!defined('HTMLY')) die('HTMLy');
  3. use \Michelf\MarkdownExtra;
  4. use \Suin\RSSWriter\Feed;
  5. use \Suin\RSSWriter\Channel;
  6. use \Suin\RSSWriter\Item;
  7. // Get blog post with more info about the path. Sorted by filename.
  8. function get_blog_posts()
  9. {
  10. static $_posts = array();
  11. $url = 'cache/index/index-posts.txt';
  12. if (!file_exists($url)) {
  13. rebuilt_cache('all');
  14. }
  15. $_posts = unserialize(file_get_contents($url));
  16. return $_posts;
  17. }
  18. // Get static page path.
  19. function get_static_pages()
  20. {
  21. static $_page = array();
  22. $url = 'cache/index/index-pages.txt';
  23. if (!file_exists($url)) {
  24. rebuilt_cache('all');
  25. }
  26. $_page = unserialize(file_get_contents($url));
  27. return $_page;
  28. }
  29. // Get static subpage path.
  30. function get_static_subpages($static = null)
  31. {
  32. static $_sub_page = array();
  33. $url = 'cache/index/index-subpages.txt';
  34. if (!file_exists($url)) {
  35. rebuilt_cache('all');
  36. }
  37. $_sub_page = unserialize(file_get_contents($url));
  38. if ($static != null) {
  39. $stringLen = strlen($static);
  40. return array_filter($_sub_page, function ($sub_page) use ($static, $stringLen) {
  41. $x = str_replace('content/static/', '', $sub_page['dirname']);
  42. $y = explode('.', $x);
  43. if (isset($y[1])) {
  44. $z = $y[1];
  45. } else {
  46. $z = $x;
  47. }
  48. if ($z == $static) {
  49. return true;
  50. }
  51. return false;
  52. });
  53. }
  54. return $_sub_page;
  55. }
  56. // Get author name.
  57. function get_author_name()
  58. {
  59. static $_author = array();
  60. $url = 'cache/index/index-author.txt';
  61. if (!file_exists($url)) {
  62. rebuilt_cache('all');
  63. }
  64. $_author = unserialize(file_get_contents($url));
  65. return $_author;
  66. }
  67. // Get posts draft.
  68. function get_draft_posts()
  69. {
  70. static $_draft = array();
  71. $url = 'cache/index/index-draft.txt';
  72. if (!file_exists($url)) {
  73. rebuilt_cache('all');
  74. }
  75. $_draft = unserialize(file_get_contents($url));
  76. return $_draft;
  77. }
  78. // Get static page draft.
  79. function get_draft_pages()
  80. {
  81. static $_draftPage = array();
  82. $tmp = array();
  83. $tmp = glob('content/static/draft/*.md', GLOB_NOSORT);
  84. if (is_array($tmp)) {
  85. foreach ($tmp as $file) {
  86. $_draftPage[] = pathinfo($file);
  87. }
  88. }
  89. usort($_draftPage, "sortfile_a");
  90. return $_draftPage;
  91. }
  92. // Get static subpage draft.
  93. function get_draft_subpages($static = null)
  94. {
  95. static $_draftSubpage = array();
  96. $tmp = array();
  97. $tmp = glob('content/static/*/draft/*.md', GLOB_NOSORT);
  98. if (is_array($tmp)) {
  99. foreach ($tmp as $file) {
  100. $_draftSubpage[] = pathinfo($file);
  101. }
  102. }
  103. usort($_draftSubpage, "sortfile_a");
  104. if ($static != null) {
  105. $stringLen = strlen($static);
  106. return array_filter($_draftSubpage, function ($sub_page) use ($static, $stringLen) {
  107. $x = explode('/', $sub_page['dirname']);
  108. $y = explode('.', $x[2]);
  109. if (isset($y[1])) {
  110. $z = $y[1];
  111. } else {
  112. $z = $y[0];
  113. }
  114. if ($z == $static) {
  115. return true;
  116. }
  117. return false;
  118. });
  119. }
  120. return $_draftSubpage;
  121. }
  122. // Get scheduled posts.
  123. function get_scheduled_posts()
  124. {
  125. static $_scheduled = array();
  126. $url = 'cache/index/index-scheduled.txt';
  127. if (!file_exists($url)) {
  128. rebuilt_cache('all');
  129. }
  130. $_scheduled = unserialize(file_get_contents($url));
  131. return $_scheduled;
  132. }
  133. // Get category info files.
  134. function get_category_files()
  135. {
  136. static $_desc = array();
  137. $url = 'cache/index/index-category-files.txt';
  138. if (!file_exists($url)) {
  139. rebuilt_cache('all');
  140. }
  141. $_desc = unserialize(file_get_contents($url));
  142. return $_desc;
  143. }
  144. // Get category folder.
  145. function get_category_folder()
  146. {
  147. static $_dfolder = array();
  148. $tmp = array();
  149. $tmp = glob('content/*/blog/*/', GLOB_ONLYDIR);
  150. if (is_array($tmp)) {
  151. foreach ($tmp as $dir) {
  152. $_dfolder[] = $dir;
  153. }
  154. }
  155. return $_dfolder;
  156. }
  157. // Get category slug.
  158. function get_category_slug()
  159. {
  160. static $_cslug = array();
  161. $url = 'cache/index/index-category.txt';
  162. if (!file_exists($url)) {
  163. rebuilt_cache('all');
  164. }
  165. $_cslug = unserialize(file_get_contents($url));
  166. return $_cslug;
  167. }
  168. // Get backup file.
  169. function get_zip_files()
  170. {
  171. static $_zip = array();
  172. $_zip = glob('backup/*.zip');
  173. return $_zip;
  174. }
  175. // Get images in content/images folder
  176. function scan_images() {
  177. static $_images = array();
  178. $tmp = array();
  179. $tmp = array_filter(glob('content/images/*', GLOB_NOSORT), 'is_file');
  180. if (is_array($tmp)) {
  181. foreach ($tmp as $file) {
  182. $_images[] = pathinfo($file);
  183. }
  184. }
  185. usort($_images, "sortfile_d");
  186. return $_images;
  187. }
  188. // usort function. Sort by filename.
  189. function sortfile_a($a, $b)
  190. {
  191. return $a['basename'] == $b['basename'] ? 0 : (($a['basename'] > $b['basename']) ? 1 : -1);
  192. }
  193. // usort function.
  194. function sortfile_d($a, $b)
  195. {
  196. return $a['basename'] == $b['basename'] ? 0 : (($a['basename'] < $b['basename']) ? 1 : -1);
  197. }
  198. // usort function. Sort by date.
  199. function sortdate($a, $b)
  200. {
  201. return $a->date == $b->date ? 0 : (($a->date < $b->date) ? 1 : -1);
  202. }
  203. // Rebuilt cache index
  204. function rebuilt_cache($type = null)
  205. {
  206. $dir = 'cache/index';
  207. $posts_cache = array();
  208. $page_cache = array();
  209. $subpage_cache = array();
  210. $author_cache = array();
  211. $scheduled_cache = array();
  212. $category_cache = array();
  213. $draft_cache = array();
  214. if (!is_dir($dir)) {
  215. mkdir($dir, 0775, true);
  216. }
  217. // Rebuilt posts index
  218. $tmp = array();
  219. $ctmp = array();
  220. $tmp = glob('content/*/blog/*/*/*.md', GLOB_NOSORT);
  221. if (is_array($tmp)) {
  222. foreach ($tmp as $file) {
  223. if(strpos($file, '/draft/') === false) {
  224. $posts_cache[] = pathinfo($file);
  225. $pc = explode('/', $file);
  226. $ctmp[] = $pc[3];
  227. }
  228. }
  229. }
  230. usort($posts_cache, "sortfile_d");
  231. $posts_string = serialize($posts_cache);
  232. file_put_contents('cache/index/index-posts.txt', print_r($posts_string, true), LOCK_EX);
  233. // Rebuilt scheduled posts index
  234. $stmp = array();
  235. $stmp = glob('content/*/*/*/*/scheduled/*.md', GLOB_NOSORT);
  236. if (is_array($stmp)) {
  237. foreach ($stmp as $file) {
  238. $scheduled_cache[] = pathinfo($file);
  239. $ss = explode('/', $file);
  240. $ctmp[] = $ss[3];
  241. }
  242. }
  243. usort($scheduled_cache, "sortfile_d");
  244. $scheduled_string = serialize($scheduled_cache);
  245. file_put_contents('cache/index/index-scheduled.txt', print_r($scheduled_string, true), LOCK_EX);
  246. // Rebuilt draft posts index
  247. $drf = array();
  248. $drf = glob('content/*/*/*/draft/*.md', GLOB_NOSORT);
  249. if (is_array($drf)) {
  250. foreach ($drf as $file) {
  251. $draft_cache[] = pathinfo($file);
  252. $dd = explode('/', $file);
  253. $ctmp[] = $dd[3];
  254. }
  255. }
  256. usort($draft_cache, "sortfile_d");
  257. $draft_string = serialize($draft_cache);
  258. file_put_contents('cache/index/index-draft.txt', print_r($draft_string, true), LOCK_EX);
  259. // Rebuilt category files index
  260. $ftmp = array();
  261. $ftmp = glob('content/data/category/*.md', GLOB_NOSORT);
  262. if (is_array($ftmp)) {
  263. foreach ($ftmp as $file) {
  264. $category_cache[] = pathinfo($file);
  265. $ctmp[] = pathinfo($file, PATHINFO_FILENAME);
  266. }
  267. }
  268. usort($category_cache, "sortfile_a");
  269. $category_string = serialize($category_cache);
  270. file_put_contents('cache/index/index-category-files.txt', print_r($category_string, true), LOCK_EX);
  271. // Rebuilt category slug index
  272. $dirc = array();
  273. $dirc = array_push($ctmp, 'uncategorized');
  274. $dirc = array_unique($ctmp, SORT_REGULAR);
  275. file_put_contents('cache/index/index-category.txt', print_r(serialize($dirc), true), LOCK_EX);
  276. // Rebuilt static page index
  277. $ptmp = array();
  278. $ptmp = glob('content/static/*.md', GLOB_NOSORT);
  279. natsort($ptmp);
  280. if (is_array($ptmp)) {
  281. foreach ($ptmp as $file) {
  282. if(strpos($file, '/draft/') === false) {
  283. $page_cache[] = pathinfo($file);
  284. }
  285. }
  286. }
  287. $page_string = serialize($page_cache);
  288. file_put_contents('cache/index/index-pages.txt', print_r($page_string, true), LOCK_EX);
  289. // Rebuilt subpage index
  290. $sptmp = array();
  291. $sptmp = glob('content/static/*/*.md', GLOB_NOSORT);
  292. natsort($sptmp);
  293. if (is_array($sptmp)) {
  294. foreach ($sptmp as $file) {
  295. if(strpos($file, '/draft/') === false) {
  296. $subpage_cache[] = pathinfo($file);
  297. }
  298. }
  299. }
  300. $subpage_string = serialize($subpage_cache);
  301. file_put_contents('cache/index/index-subpages.txt', print_r($subpage_string, true), LOCK_EX);
  302. // Rebuilt user profile index
  303. $atmp = array();
  304. $atmp = glob('content/*/author.md', GLOB_NOSORT);
  305. if (is_array($atmp)) {
  306. foreach ($atmp as $file) {
  307. $author_cache[] = pathinfo($file);
  308. }
  309. }
  310. usort($author_cache, "sortfile_a");
  311. $author_string = serialize($author_cache);
  312. file_put_contents('cache/index/index-author.txt', print_r($author_string, true), LOCK_EX);
  313. // Remove the widget cache
  314. foreach (glob('cache/widget/*.cache', GLOB_NOSORT) as $file) {
  315. unlink($file);
  316. }
  317. }
  318. // Return blog posts.
  319. function get_posts($posts, $page = 1, $perpage = 0)
  320. {
  321. if (empty($posts)) {
  322. $posts = get_blog_posts();
  323. }
  324. $tmp = array();
  325. $views = array();
  326. // Extract a specific page with results
  327. $posts = array_slice($posts, ($page - 1) * $perpage, $perpage);
  328. $cList = category_list(true);
  329. $auto = config('toc.automatic');
  330. $counter = config('views.counter');
  331. $viewsFile = "content/data/views.json";
  332. if (file_exists($viewsFile) && $counter == 'true') {
  333. $views = json_decode(file_get_contents($viewsFile), true);
  334. }
  335. foreach ($posts as $index => $v) {
  336. $post = new stdClass;
  337. $filepath = $v['dirname'] . '/' . $v['basename'];
  338. // Extract the date
  339. $arr = explode('_', $v['basename']);
  340. // dirname string
  341. $dirname = $v['dirname'];
  342. // Author string
  343. $str = explode('/', $dirname);
  344. $author = $str[1];
  345. foreach ($cList as $a => $t) {
  346. if ($t['0'] === $str[3]) {
  347. $post->category = '<a href="' . site_url() . 'category/' . $t['0'] . '">' . $t['1'] . '</a>';
  348. $post->categoryUrl = site_url() . 'category/' . $t['0'];
  349. $post->categoryCount = $t['2'];
  350. $post->categorySlug = $t['0'];
  351. $post->categoryMd = $t['0'] . '.md';
  352. $post->categoryTitle = $t['1'];
  353. $post->categoryb = '<a itemprop="item" href="' . site_url() . 'category/' . $t['0'] . '"><span itemprop="name">' . $t['1'] . '</span></a>';
  354. }
  355. }
  356. $type = $str[4];
  357. $post->ct = $str[3];
  358. // The post author + author url
  359. $post->author = $author;
  360. $post->authorUrl = site_url() . 'author/' . $author;
  361. $post->authorRss = site_url() . 'author/' . $author . '/feed';
  362. $profile = get_author($author);
  363. if (isset($profile[0])) {
  364. $post->authorName = $profile[0]->name;
  365. $post->authorDescription = $profile[0]->description;
  366. $post->authorAbout = $profile[0]->about;
  367. $post->authorAvatar = $profile[0]->avatar;
  368. $post->authorRaw = $profile[0]->raw;
  369. } else {
  370. $post->authorName = $author;
  371. $post->authorDescription = i18n('Author_Description');
  372. $post->authorAbout = $post->authorDescription;
  373. $post->authorAvatar = site_url() . 'system/resources/images/logo-small.png';
  374. $post->authorRaw = $post->authorDescription;
  375. }
  376. $post->type = $type;
  377. $dt = str_replace($dirname, '', $arr[0]);
  378. $t = str_replace('-', '', $dt);
  379. $time = new DateTime($t);
  380. $timestamp = $time->format("Y-m-d H:i:s");
  381. // The post date
  382. $post->date = strtotime($timestamp);
  383. $post->lastMod = strtotime(date('Y-m-d H:i:s', filemtime($filepath)));
  384. // The archive per day
  385. $post->archive = site_url() . 'archive/' . date('Y-m', $post->date);
  386. if (permalink_type() == 'default') {
  387. $post->url = site_url() . date('Y/m', $post->date) . '/' . str_replace('.md', '', $arr[2]);
  388. } else {
  389. $post->url = site_url() . permalink_type() . '/' . str_replace('.md', '', $arr[2]);
  390. }
  391. $post->slug = str_replace('.md', '', $arr[2]);
  392. $post->file = $filepath;
  393. $content = file_get_contents($filepath);
  394. // Extract the title and body
  395. $post->title = get_content_tag('t', $content, 'Untitled post: ' . format_date($post->lastMod, 'l, j F Y, H:i'));
  396. $post->image = get_content_tag('image', $content);
  397. $post->video = get_content_tag('video', $content);
  398. $post->link = get_content_tag('link', $content);
  399. $post->quote = get_content_tag('quote', $content);
  400. $post->audio = get_content_tag('audio', $content);
  401. $post->ai = get_content_tag('ai', $content);
  402. $tag = array();
  403. $url = array();
  404. $bc = array();
  405. $rel = array();
  406. $tagt = get_content_tag('tag', $content);
  407. $t = explode(',', rtrim($arr[1], ','));
  408. if(!empty($tagt)) {
  409. $tl = explode(',', rtrim($tagt, ','));
  410. if (count($tl) == count($t)) {
  411. $tCom = array_combine($t, $tl);
  412. } else {
  413. $tCom = array_combine($t, $t);
  414. }
  415. foreach ($tCom as $key => $val) {
  416. if(!empty($val)) {
  417. $tag[] = array($val, site_url() . 'tag/' . strtolower($key));
  418. } else {
  419. $tag[] = array($key, site_url() . 'tag/' . strtolower($key));
  420. }
  421. }
  422. } else {
  423. foreach ($t as $tt) {
  424. $tag[] = array($tt, site_url() . 'tag/' . strtolower($tt));
  425. }
  426. }
  427. foreach ($tag as $a) {
  428. $url[] = '<a rel="tag" href="' . $a[1] . '">' . $a[0] . '</a>';
  429. $bc[] = '<span><a href="' . $a[1] . '">' . $a[0] . '</a></span>';
  430. }
  431. $post->tag = implode(' ', $url);
  432. $post->tagb = implode(' » ', $bc);
  433. $post->related = $post->categorySlug. ',' .$post->url;
  434. $more = explode('<!--more-->', $content);
  435. if (isset($more['1'])) {
  436. $content = $more['0'] . '<!--more--><div class="more-wrapper"><a id="more"></a></div>' . $more['1'];
  437. }
  438. // Get the contents and convert it to HTML
  439. $post->body = MarkdownExtra::defaultTransform(remove_html_comments($content));
  440. $post->description = get_content_tag("d", $content, get_description($post->body));
  441. $word_count = str_word_count(strip_tags($post->body));
  442. $post->readTime = ceil($word_count / 200);
  443. $toc = explode('<!--toc-->', $post->body);
  444. if (isset($toc['1'])) {
  445. $post->body = insert_toc('post-' . $post->date, $toc['0'], $toc['1']);
  446. } else {
  447. if ($auto === 'true') {
  448. $post->body = automatic_toc($post->body, 'post-' . $post->date);
  449. }
  450. }
  451. if ($counter == 'true') {
  452. $post->views = get_views('post_' . $post->slug, $views);
  453. } else {
  454. $post->views = null;
  455. }
  456. $post->raw = $content;
  457. $tmp[] = $post;
  458. }
  459. return $tmp;
  460. }
  461. function get_pages($pages, $page = 1, $perpage = 0)
  462. {
  463. if (empty($pages)) {
  464. $pages = get_static_pages();
  465. }
  466. $tmp = array();
  467. $auto = config('toc.automatic');
  468. $counter = config('views.counter');
  469. $viewsFile = "content/data/views.json";
  470. if (file_exists($viewsFile) && $counter == 'true') {
  471. $views = json_decode(file_get_contents($viewsFile), true);
  472. }
  473. // Extract a specific page with results
  474. $pages = array_slice($pages, ($page - 1) * $perpage, $perpage);
  475. foreach ($pages as $index => $v) {
  476. $post = new stdClass;
  477. // The static page URL
  478. $fn = explode('.', $v['filename']);
  479. if (isset($fn[1])) {
  480. $url = $fn[1];
  481. } else {
  482. $url= $v['filename'];
  483. }
  484. $post->url = site_url() . $url;
  485. $post->file = $v['dirname'] . '/' . $v['basename'];
  486. $post->lastMod = strtotime(date('Y-m-d H:i:s', filemtime($post->file)));
  487. $post->md = $v['basename'];
  488. $post->slug = $url;
  489. $post->parent = null;
  490. $post->parentSlug = null;
  491. // Get the contents and convert it to HTML
  492. $content = file_get_contents($post->file);
  493. // Extract the title and body
  494. $post->title = get_content_tag('t', $content, 'Untitled static page: ' . format_date($post->lastMod, 'l, j F Y, H:i'));
  495. // Get the contents and convert it to HTML
  496. $post->body = MarkdownExtra::defaultTransform(remove_html_comments($content));
  497. $post->description = get_content_tag("d", $content, get_description($post->body));
  498. $word_count = str_word_count(strip_tags($post->body));
  499. $post->readTime = ceil($word_count / 200);
  500. $toc = explode('<!--toc-->', $post->body);
  501. if (isset($toc['1'])) {
  502. $post->body = insert_toc('page-' . $post->slug, $toc['0'], $toc['1']);
  503. } else {
  504. if ($auto === 'true') {
  505. $post->body = automatic_toc($post->body, 'page-' . $post->slug);
  506. }
  507. }
  508. if ($counter == 'true') {
  509. $post->views = get_views('page_' . $post->slug, $views);
  510. } else {
  511. $post->views = null;
  512. }
  513. $post->raw = $content;
  514. $tmp[] = $post;
  515. }
  516. return $tmp;
  517. }
  518. function get_subpages($sub_pages, $page = 1, $perpage = 0)
  519. {
  520. if (empty($sub_pages)) {
  521. $sub_pages = get_static_subpages();
  522. }
  523. $tmp = array();
  524. $auto = config('toc.automatic');
  525. $counter = config('views.counter');
  526. $viewsFile = "content/data/views.json";
  527. if (file_exists($viewsFile) && $counter == 'true') {
  528. $views = json_decode(file_get_contents($viewsFile), true);
  529. }
  530. // Extract a specific page with results
  531. $sub_pages = array_slice($sub_pages, ($page - 1) * $perpage, $perpage);
  532. foreach ($sub_pages as $index => $v) {
  533. $post = new stdClass;
  534. $fd = str_replace(dirname($v['dirname']) . '/', '', $v['dirname']);
  535. $st = explode('.', $fd);
  536. if (isset($st[1])) {
  537. $static = $st[1];
  538. } else {
  539. $static = $fd;
  540. }
  541. // The static page URL
  542. $fn = explode('.', $v['filename']);
  543. if (isset($fn[1])) {
  544. $url = $fn[1];
  545. } else {
  546. $url= $v['filename'];
  547. }
  548. $post->url = site_url() . $static . "/" . $url;
  549. $post->file = $v['dirname'] . '/' . $v['basename'];
  550. $post->lastMod = strtotime(date('Y-m-d H:i:s', filemtime($post->file)));
  551. $post->md = $v['basename'];
  552. $post->slug = $url;
  553. $post->parent = $fd;
  554. $post->parentSlug = $static;
  555. // Get the contents and convert it to HTML
  556. $content = file_get_contents($post->file);
  557. // Extract the title and body
  558. $post->title = get_content_tag('t', $content, 'Untitled static subpage: ' . format_date($post->lastMod, 'l, j F Y, H:i'));
  559. // Get the contents and convert it to HTML
  560. $post->body = MarkdownExtra::defaultTransform(remove_html_comments($content));
  561. $post->description = get_content_tag("d", $content, get_description($post->body));
  562. $word_count = str_word_count(strip_tags($post->body));
  563. $post->readTime = ceil($word_count / 200);
  564. $toc = explode('<!--toc-->', $post->body);
  565. if (isset($toc['1'])) {
  566. $post->body = insert_toc('subpage-' . $post->slug, $toc['0'], $toc['1']);
  567. } else {
  568. if ($auto === 'true') {
  569. $post->body = automatic_toc($post->body, 'subpage-' . $post->slug);
  570. }
  571. }
  572. if ($counter == 'true') {
  573. $post->views = get_views('subpage_' . $post->parentSlug .'.'. $post->slug, $views);
  574. } else {
  575. $post->views = null;
  576. }
  577. $post->raw = $content;
  578. $tmp[] = $post;
  579. }
  580. return $tmp;
  581. }
  582. // Find post by year, month and name, previous, and next.
  583. function find_post($year, $month, $name)
  584. {
  585. $posts = get_blog_posts();
  586. foreach ($posts as $index => $v) {
  587. $arr = explode('_', $v['basename']);
  588. if ((strpos($arr[0], "$year-$month") !== false && strtolower($arr[2]) === strtolower($name . '.md')) || ($year === NULL && strtolower($arr[2]) === strtolower($name . '.md'))) {
  589. // Use the get_posts method to return
  590. // a properly parsed object
  591. $ar = get_posts($posts, $index + 1, 1);
  592. $nx = get_posts($posts, $index, 1);
  593. $pr = get_posts($posts, $index + 2, 1);
  594. if ($index == 0) {
  595. if (isset($pr[0])) {
  596. return array(
  597. 'current' => $ar[0],
  598. 'prev' => $pr[0]
  599. );
  600. } else {
  601. return array(
  602. 'current' => $ar[0],
  603. 'prev' => null
  604. );
  605. }
  606. } elseif (count($posts) == $index + 1) {
  607. return array(
  608. 'current' => $ar[0],
  609. 'next' => $nx[0]
  610. );
  611. } else {
  612. return array(
  613. 'current' => $ar[0],
  614. 'next' => $nx[0],
  615. 'prev' => $pr[0]
  616. );
  617. }
  618. }
  619. }
  620. }
  621. // Return static page.
  622. function find_page($static = null, $pages = null)
  623. {
  624. if (empty($pages)) {
  625. $pages = get_static_pages();
  626. }
  627. $tmp = array();
  628. if (!empty($pages)) {
  629. foreach ($pages as $index => $v) {
  630. if (is_null($static)) {
  631. return get_pages($pages, 1, null);
  632. } elseif (stripos($v['basename'], $static . '.md') !== false) {
  633. // Use the get_posts method to return
  634. // a properly parsed object
  635. $ar = get_pages($pages, $index + 1, 1);
  636. $nx = get_pages($pages, $index, 1);
  637. $pr = get_pages($pages, $index + 2, 1);
  638. if ($index == 0) {
  639. if (isset($pr[0])) {
  640. return array(
  641. 'current' => $ar[0],
  642. 'prev' => $pr[0]
  643. );
  644. } else {
  645. return array(
  646. 'current' => $ar[0],
  647. 'prev' => null
  648. );
  649. }
  650. } elseif (count($pages) == $index + 1) {
  651. return array(
  652. 'current' => $ar[0],
  653. 'next' => $nx[0]
  654. );
  655. } else {
  656. return array(
  657. 'current' => $ar[0],
  658. 'next' => $nx[0],
  659. 'prev' => $pr[0]
  660. );
  661. }
  662. }
  663. }
  664. }
  665. return $tmp;
  666. }
  667. // Return static subpage.
  668. function find_subpage($static, $sub_static = null, $sub_pages = null)
  669. {
  670. if (empty($sub_pages)) {
  671. $sub_pages = array_values(get_static_subpages($static));
  672. }
  673. $tmp = array();
  674. if (!empty($sub_pages)) {
  675. foreach ($sub_pages as $index => $v) {
  676. if (is_null($sub_static)) {
  677. return get_subpages($sub_pages, 1, null);
  678. } elseif (stripos($v['basename'], $sub_static . '.md') !== false) {
  679. // Use the get_posts method to return
  680. // a properly parsed object
  681. $ar = get_subpages($sub_pages, $index + 1, 1);
  682. $nx = get_subpages($sub_pages, $index, 1);
  683. $pr = get_subpages($sub_pages, $index + 2, 1);
  684. if ($index == 0) {
  685. if (isset($pr[0])) {
  686. return array(
  687. 'current' => $ar[0],
  688. 'prev' => $pr[0]
  689. );
  690. } else {
  691. return array(
  692. 'current' => $ar[0],
  693. 'prev' => null
  694. );
  695. }
  696. } elseif (count($sub_pages) == $index + 1) {
  697. return array(
  698. 'current' => $ar[0],
  699. 'next' => $nx[0]
  700. );
  701. } else {
  702. return array(
  703. 'current' => $ar[0],
  704. 'next' => $nx[0],
  705. 'prev' => $pr[0]
  706. );
  707. }
  708. }
  709. }
  710. }
  711. return $tmp;
  712. }
  713. // Return category page.
  714. function get_category($category, $page, $perpage, $random = null)
  715. {
  716. if (is_null($category)) return array();
  717. $posts = get_blog_posts();
  718. if ($random === true) {
  719. shuffle($posts);
  720. }
  721. $tmp = array();
  722. if (empty($perpage)) {
  723. $perpage = 10;
  724. }
  725. foreach ($posts as $index => $v) {
  726. // dirname string
  727. $dirname = $v['dirname'];
  728. $str = explode('/', $dirname);
  729. if (strtolower($category) === strtolower($str[3])) {
  730. $tmp[] = $v;
  731. }
  732. }
  733. if (empty($tmp)) {
  734. return $tmp;
  735. }
  736. $tmp = array_unique($tmp, SORT_REGULAR);
  737. return $tmp = array(get_posts($tmp, $page, $perpage), count($tmp));
  738. }
  739. // Return category info.
  740. function get_category_info($category = null)
  741. {
  742. $tmp = array();
  743. $cslug= get_category_slug();
  744. if (!empty($cslug)) {
  745. asort($cslug);
  746. if (is_null($category)) {
  747. foreach ($cslug as $key => $c){
  748. $ctmp = read_category_info($c);
  749. if (!empty($ctmp[0])) {
  750. $tmp[] = $ctmp[0];
  751. } else {
  752. $tmp[] = default_category($c);
  753. }
  754. }
  755. } else {
  756. foreach ($cslug as $key => $c){
  757. if ($c === $category) {
  758. $ctmp = read_category_info($category);
  759. if (!empty($ctmp[0])) {
  760. $tmp[] = $ctmp[0];
  761. } else {
  762. $tmp[] = default_category($category);
  763. }
  764. }
  765. }
  766. }
  767. } else {
  768. $tmp[] = default_category($category);
  769. }
  770. return $tmp;
  771. }
  772. function read_category_info($category)
  773. {
  774. $tmp = array();
  775. $cFiles = get_category_files();
  776. if (!empty($cFiles)) {
  777. foreach ($cFiles as $index => $v) {
  778. if ($v['basename'] == $category . '.md' ) {
  779. $desc = new stdClass;
  780. // The filename
  781. $filename = $v['dirname'] . '/' . $v['basename'];
  782. $url= $v['filename'];
  783. $desc->url = site_url() . 'category/' . $url;
  784. $desc->md = $v['basename'];
  785. $desc->slug = $url;
  786. $desc->count = get_categorycount($url);
  787. $desc->file = $filename;
  788. $desc->rss = $desc->url . '/feed';
  789. // Get the contents and convert it to HTML
  790. $content = file_get_contents($desc->file);
  791. // Extract the title and body
  792. $desc->title = get_content_tag('t', $content, $category);
  793. // Get the contents and convert it to HTML
  794. $desc->body = MarkdownExtra::defaultTransform(remove_html_comments($content));
  795. $desc->description = get_content_tag("d", $content, get_description($desc->body));
  796. $toc = explode('<!--toc-->', $desc->body);
  797. if (isset($toc['1'])) {
  798. $desc->body = insert_toc('taxonomy-' . $desc->slug, $toc['0'], $toc['1']);
  799. }
  800. $desc->raw = $content;
  801. $tmp[] = $desc;
  802. }
  803. }
  804. }
  805. return $tmp;
  806. }
  807. // Return default category
  808. function default_category($category = null)
  809. {
  810. $tmp = array();
  811. $desc = new stdClass;
  812. if (is_null($category) || $category == 'uncategorized') {
  813. $desc->title = i18n("Uncategorized");
  814. $desc->url = site_url() . 'category/uncategorized';
  815. $desc->slug = 'uncategorized';
  816. $desc->body = '<p>' . i18n('Uncategorized_comment') . '</p>';
  817. $desc->md = 'uncategorized.md';
  818. $desc->description = i18n('Uncategorized_comment');
  819. $desc->file = '';
  820. $desc->count = get_categorycount($desc->slug);
  821. $desc->rss = $desc->url . '/feed';
  822. $desc->raw = $desc->body;
  823. } else{
  824. $desc->title = $category;
  825. $desc->url = site_url() . 'category/' . $category;
  826. $desc->slug = $category;
  827. $desc->body = '<p>' . i18n('All_blog_posts') . ': ' . $category . '</p>';
  828. $desc->md = $category . '.md';
  829. $desc->description = i18n('All_blog_posts') . ': ' . $category;
  830. $desc->file = '';
  831. $desc->count = get_categorycount($category);
  832. $desc->rss = $desc->url . '/feed';
  833. $desc->raw = $desc->body;
  834. }
  835. return $tmp[] = $desc;
  836. }
  837. // Return category list
  838. function category_list($custom = null)
  839. {
  840. $dir = "cache/widget";
  841. $filename = "cache/widget/category.list.cache";
  842. $tmp = array();
  843. $cat = array();
  844. $list = array();
  845. $cList = '';
  846. if (!is_dir($dir)) {
  847. mkdir($dir, 0775, true);
  848. }
  849. if (file_exists($filename)) {
  850. $cat = unserialize(file_get_contents($filename));
  851. } else {
  852. $arr = get_category_info(null);
  853. foreach ($arr as $i => $a) {
  854. $cat[] = array($a->slug, $a->title, $a->count, $a->description, $a->body, $a->raw, $a->file, $a->rss);
  855. }
  856. $tmp = serialize($cat);
  857. file_put_contents($filename, print_r($tmp, true), LOCK_EX);
  858. }
  859. if(!empty($custom)) {
  860. return $cat;
  861. }
  862. $cList .= '<ul>';
  863. foreach ($cat as $k => $v) {
  864. if ($v['2'] !== 0) {
  865. $cList .= '<li><a href="' . site_url() . 'category/' . $v['0'] . '">' . $v['1'] . '</a> <span>('. $v['2'] .')</span></li>';
  866. }
  867. }
  868. $cList .= '</ul>';
  869. return $cList;
  870. }
  871. // Return type page.
  872. function get_type($type, $page, $perpage)
  873. {
  874. if (is_null($type)) return array();
  875. $posts = get_blog_posts();
  876. $tmp = array();
  877. if (empty($perpage)) {
  878. $perpage = 10;
  879. }
  880. foreach ($posts as $index => $v) {
  881. // dirname string
  882. $dirname = $v['dirname'];
  883. $str = explode('/', $dirname);
  884. if (strtolower($type) === strtolower($str[4])) {
  885. $tmp[] = $v;
  886. }
  887. }
  888. if (empty($tmp)) {
  889. return $tmp;
  890. }
  891. $tmp = array_unique($tmp, SORT_REGULAR);
  892. return $tmp = array(get_posts($tmp, $page, $perpage), count($tmp));
  893. }
  894. // Return tag page.
  895. function get_tag($tag, $page, $perpage, $random = null)
  896. {
  897. if (is_null($tag)) return array();
  898. $posts = get_blog_posts();
  899. if ($random === true) {
  900. shuffle($posts);
  901. }
  902. $tmp = array();
  903. foreach ($posts as $index => $v) {
  904. $str = explode('_', $v['basename']);
  905. $mtag = explode(',', rtrim($str[1], ','));
  906. $etag = explode(',', $tag);
  907. foreach ($mtag as $t) {
  908. foreach ($etag as $e) {
  909. $e = trim($e);
  910. if (strtolower($t) === strtolower($e)) {
  911. $tmp[] = $v;
  912. }
  913. }
  914. }
  915. }
  916. if (empty($tmp)) {
  917. return $tmp;
  918. }
  919. $tmp = array_unique($tmp, SORT_REGULAR);
  920. return $tmp = array(get_posts($tmp, $page, $perpage), count($tmp));
  921. }
  922. // Return archive page.
  923. function get_archive($req, $page, $perpage)
  924. {
  925. if (is_null($req)) return array();
  926. $posts = get_blog_posts();
  927. $tmp = array();
  928. foreach ($posts as $index => $v) {
  929. $str = explode('_', $v['basename']);
  930. if (strpos($str[0], "$req") !== false) {
  931. $tmp[] = $v;
  932. }
  933. }
  934. if (empty($tmp)) {
  935. return $tmp;
  936. }
  937. return $tmp = array(get_posts($tmp, $page, $perpage), count($tmp));
  938. }
  939. // Return posts list on profile.
  940. function get_profile_posts($name, $page, $perpage)
  941. {
  942. if (is_null($name)) return array();
  943. $posts = get_blog_posts();
  944. $tmp = array();
  945. foreach ($posts as $index => $v) {
  946. $str = explode('/', $v['dirname']);
  947. if (strtolower($name) === strtolower($str[1])) {
  948. $tmp[] = $v;
  949. }
  950. }
  951. if (empty($tmp)) {
  952. return $tmp;
  953. }
  954. return $tmp = array(get_posts($tmp, $page, $perpage), count($tmp));
  955. }
  956. // Return author info.
  957. function get_author($name)
  958. {
  959. if (is_null($name)) return array();
  960. $names = get_author_name();
  961. $tmp = array();
  962. if (!empty($names)) {
  963. foreach ($names as $index => $v) {
  964. $author = new stdClass;
  965. // dirname string
  966. $dirname = $v['dirname'];
  967. // Author string
  968. $str = explode('/', $dirname);
  969. $profile = $str[1];
  970. if ($name === $profile) {
  971. // Profile URL
  972. $filename = $v['dirname'] . '/' . $v['basename'];
  973. $author->file = $filename;
  974. $author->url = site_url() . 'author/' . $profile;
  975. $author->slug = $profile;
  976. // Get the contents and convert it to HTML
  977. $content = file_get_contents($author->file);
  978. // Extract the title and body
  979. $author->name = get_content_tag('t', $content, $author);
  980. // Get the contents and convert it to HTML
  981. $author->about = MarkdownExtra::defaultTransform(remove_html_comments($content));
  982. $author->description = get_content_tag("d", $content, get_description($author->about));
  983. $author->avatar = get_content_tag("image", $content, site_url() . 'system/resources/images/logo-small.png');
  984. $author->rss = $author->url . '/feed';
  985. $toc = explode('<!--toc-->', $author->about);
  986. if (isset($toc['1'])) {
  987. $author->about = insert_toc('profile-' . $author->slug, $toc['0'], $toc['1']);
  988. }
  989. $author->body = $author->about;
  990. $author->title = $author->name;
  991. $author->raw = $content;
  992. $tmp[] = $author;
  993. }
  994. }
  995. }
  996. return $tmp;
  997. }
  998. // Return default profile
  999. function default_profile($name)
  1000. {
  1001. $tmp = array();
  1002. $author = new stdClass;
  1003. $author->name = $name;
  1004. $author->title = $name;
  1005. $author->about = '<p>' . i18n('Author_Description') . '</p>';
  1006. $author->body = '<p>' . i18n('Author_Description') . '</p>';
  1007. $author->description = i18n('Author_Description');
  1008. $author->avatar = site_url() . 'system/resources/images/logo-small.png';
  1009. $author->url = site_url(). 'author/' . $name;
  1010. $author->slug = $name;
  1011. $author->file = '';
  1012. $author->rss = $author->url . '/feed';
  1013. $author->raw = $author->about;
  1014. return $tmp[] = $author;
  1015. }
  1016. // Return frontpage content
  1017. function get_frontpage()
  1018. {
  1019. $front = new stdClass;
  1020. $filename = 'content/data/frontpage/frontpage.md';
  1021. if (file_exists($filename)) {
  1022. $content = file_get_contents($filename);
  1023. $front->file = $filename;
  1024. $front->title = get_content_tag('t', $content, 'Welcome');
  1025. $front->url = site_url() . 'front';
  1026. $front->slug = 'front';
  1027. $front->parent = null;
  1028. $front->parentSlug = null;
  1029. // Get the contents and convert it to HTML
  1030. $front->body = MarkdownExtra::defaultTransform(remove_html_comments($content));
  1031. $front->description = get_content_tag("d", $content, get_description($front->body));
  1032. $word_count = str_word_count(strip_tags($front->body));
  1033. $front->readTime = ceil($word_count / 200);
  1034. $front->views = null;
  1035. $toc = explode('<!--toc-->', $front->body);
  1036. if (isset($toc['1'])) {
  1037. $front->body = insert_toc('page-front', $toc['0'], $toc['1']);
  1038. }
  1039. $front->raw = $content;
  1040. } else {
  1041. $front->title = 'Welcome';
  1042. $front->url = site_url() . 'front';
  1043. $front->body = 'Welcome to our website.';
  1044. $front->file = null;
  1045. $front->slug = 'front';
  1046. $front->parent = null;
  1047. $front->parentSlug = null;
  1048. $front->description = $front->body;
  1049. $word_count = str_word_count(strip_tags($front->body));
  1050. $front->readTime = ceil($word_count / 200);
  1051. $front->views = null;
  1052. $front->raw = $front->body;
  1053. }
  1054. return $front;
  1055. }
  1056. // Return search page.
  1057. function get_keyword($keyword, $page, $perpage)
  1058. {
  1059. if (strlen($keyword) < 3) {
  1060. return array();
  1061. }
  1062. $posts = get_blog_posts();
  1063. $tmp = array();
  1064. $search = array();
  1065. $searchFile = "content/data/search.json";
  1066. if (file_exists($searchFile) && config('fulltext.search') == "true") {
  1067. $search = json_decode(file_get_contents($searchFile), true);
  1068. }
  1069. foreach ($posts as $index => $v) {
  1070. $arr = explode('_', $v['filename']);
  1071. if (isset($search['post_' . $arr[2]])) {
  1072. $filter = $search['post_' . $arr[2]];
  1073. } else {
  1074. $filter = $arr[1] . ' ' . $arr[2];
  1075. $keyword = remove_accent($keyword);
  1076. }
  1077. if (stripos($filter, trim($keyword)) !== false) {
  1078. if (!in_array($v, $tmp)) {
  1079. $tmp[] = $v;
  1080. }
  1081. }
  1082. }
  1083. if (empty($tmp)) {
  1084. return $tmp;
  1085. }
  1086. return $tmp = array(get_posts($tmp, $page, $perpage), count($tmp));
  1087. }
  1088. // Get related posts base on post category.
  1089. function get_related($tag, $custom = null, $count = null)
  1090. {
  1091. if (empty($count)) {
  1092. $count = config('related.count');
  1093. if (empty($count)) {
  1094. $count = 3;
  1095. }
  1096. }
  1097. $tmp = array();
  1098. $exp = explode(',', $tag);
  1099. $posts = get_category($exp[0], 1, $count + 1, true);
  1100. if ($posts) $posts = $posts[0];
  1101. $related = '';
  1102. $i = 1;
  1103. foreach ($posts as $post) {
  1104. if ($post->url !== $exp[1]) {
  1105. $tmp[] = $post;
  1106. if ($i++ >= $count)
  1107. break;
  1108. }
  1109. }
  1110. if (empty($custom)) {
  1111. if (!empty($tmp)) {
  1112. $related .= '<ul>';
  1113. foreach ($tmp as $post) {
  1114. $related .= '<li><a href="' . $post->url . '">' . $post->title . '</a></li>';
  1115. }
  1116. $related .= '</ul>';
  1117. return $related;
  1118. } else {
  1119. $related .= '<ul><li>' . i18n('No_related_post_found') . '</li></ul>';
  1120. return $related;
  1121. }
  1122. } else {
  1123. return $tmp;
  1124. }
  1125. }
  1126. // Return post count. Matching $var and $str provided.
  1127. function get_count($var, $str)
  1128. {
  1129. $posts = get_blog_posts();
  1130. $tmp = array();
  1131. foreach ($posts as $index => $v) {
  1132. if ($str === 'basename') {
  1133. $arr = explode('_', $v[$str]);
  1134. $url = $arr[0];
  1135. if (stripos($url, "$var") !== false) {
  1136. $tmp[] = $v;
  1137. }
  1138. } else {
  1139. if (stripos($v[$str], $var) !== false) {
  1140. $tmp[] = $v;
  1141. }
  1142. }
  1143. }
  1144. return count($tmp);
  1145. }
  1146. // Return category count. Matching $var
  1147. function get_categorycount($var)
  1148. {
  1149. $posts = get_blog_posts();
  1150. $tmp = array();
  1151. foreach ($posts as $index => $v) {
  1152. // dirname string
  1153. $dirname = $v['dirname'];
  1154. $str = explode('/', $dirname);
  1155. if (strtolower($var) === strtolower($str[3])) {
  1156. $tmp[] = $v;
  1157. }
  1158. }
  1159. return count($tmp);
  1160. }
  1161. // Return type count. Matching $var
  1162. function get_typecount($var)
  1163. {
  1164. $posts = get_blog_posts();
  1165. $tmp = array();
  1166. foreach ($posts as $index => $v) {
  1167. // dirname string
  1168. $dirname = $v['dirname'];
  1169. $str = explode('/', $dirname);
  1170. if (strtolower($var) === strtolower($str[4])) {
  1171. $tmp[] = $v;
  1172. }
  1173. }
  1174. return count($tmp);
  1175. }
  1176. // Return profile posts count. Matching $var
  1177. function get_profilecount($var)
  1178. {
  1179. $posts = get_blog_posts();
  1180. $tmp = array();
  1181. foreach ($posts as $index => $v) {
  1182. // dirname string
  1183. $dirname = $v['dirname'];
  1184. $str = explode('/', $dirname);
  1185. if (strtolower($var) === strtolower($str[1])) {
  1186. $tmp[] = $v;
  1187. }
  1188. }
  1189. return count($tmp);
  1190. }
  1191. // Return draft count. Matching $var
  1192. function get_draftcount($var)
  1193. {
  1194. $posts = get_draft_posts();
  1195. $tmp = array();
  1196. foreach ($posts as $index => $v) {
  1197. if (stripos($v['dirname'], '/' . $var . '/') !== false) {
  1198. $tmp[] = $v;
  1199. }
  1200. }
  1201. return count($tmp);
  1202. }
  1203. // Return scheduled post count. Matching $var
  1204. function get_scheduledcount($var)
  1205. {
  1206. $posts = get_scheduled_posts();
  1207. $tmp = array();
  1208. foreach ($posts as $index => $v) {
  1209. if (stripos($v['dirname'], '/' . $var . '/') !== false) {
  1210. $tmp[] = $v;
  1211. }
  1212. }
  1213. return count($tmp);
  1214. }
  1215. // Return tag count. Matching $var
  1216. function get_tagcount($var)
  1217. {
  1218. $posts = get_blog_posts();
  1219. $tmp = array();
  1220. foreach ($posts as $index => $v) {
  1221. $arr = explode('_', $v['basename']);
  1222. $mtag = explode(',', rtrim($arr[1], ','));
  1223. foreach ($mtag as $t) {
  1224. if (strtolower($t) === strtolower($var)) {
  1225. $tmp[] = $v;
  1226. }
  1227. }
  1228. }
  1229. return count($tmp);
  1230. }
  1231. // Return search result count
  1232. function keyword_count($keyword)
  1233. {
  1234. if (strlen($keyword) < 3) {
  1235. return array();
  1236. }
  1237. $posts = get_blog_posts();
  1238. $tmp = array();
  1239. $search = array();
  1240. $searchFile = "content/data/search.json";
  1241. if (file_exists($searchFile) && config('fulltext.search') == "true") {
  1242. $search = json_decode(file_get_contents($searchFile), true);
  1243. }
  1244. foreach ($posts as $index => $v) {
  1245. $arr = explode('_', $v['filename']);
  1246. if (isset($search['post_' . $arr[2]])) {
  1247. $filter = $search['post_' . $arr[2]];
  1248. } else {
  1249. $filter = $arr[1] . ' ' . $arr[2];
  1250. $keyword = remove_accent($keyword);
  1251. }
  1252. if (stripos($filter, trim($keyword)) !== false) {
  1253. if (!in_array($v, $tmp)) {
  1254. $tmp[] = $v;
  1255. }
  1256. }
  1257. }
  1258. $tmp = array_unique($tmp, SORT_REGULAR);
  1259. return count($tmp);
  1260. }
  1261. // Return recent posts
  1262. function recent_posts($custom = null, $count = null)
  1263. {
  1264. return get_recent('posts', 'list', $count, $custom);
  1265. }
  1266. // Return recent posts by category
  1267. function recent_category($category, $count = null, $custom = null)
  1268. {
  1269. return get_recent('category', $category, $count, $custom);
  1270. }
  1271. // Return recent posts by type
  1272. function recent_type($type, $count = null, $custom = null)
  1273. {
  1274. return get_recent('type', $type, $count, $custom);
  1275. }
  1276. // Return recent posts by tag
  1277. function recent_tag($tag, $count = null, $custom = null)
  1278. {
  1279. return get_recent('tag', $tag, $count, $custom);
  1280. }
  1281. // Return recent posts by author
  1282. function recent_profile_posts($name, $count = null, $custom = null)
  1283. {
  1284. return get_recent('profile', $name, $count, $custom);
  1285. }
  1286. function get_recent($filter, $var, $count = null, $custom = null)
  1287. {
  1288. if (is_null($var)) return array();
  1289. if (empty($count)) {
  1290. $count = config('recent.count');
  1291. if (empty($count)) {
  1292. $count = 5;
  1293. }
  1294. }
  1295. $dir = 'cache/widget';
  1296. $filename = 'cache/widget/recent.' . $filter . '.' . $var . '.cache';
  1297. $tmp = array();
  1298. $posts = array();
  1299. $recent = '';
  1300. if (!is_dir($dir)) {
  1301. mkdir($dir, 0775, true);
  1302. }
  1303. if (file_exists($filename)) {
  1304. $posts = unserialize(file_get_contents($filename));
  1305. if (count($posts) < $count) {
  1306. if ($filter == 'profile') {
  1307. $posts = get_profile_posts($var, 1, $count);
  1308. if ($posts) $posts = $posts[0];
  1309. } elseif ($filter == 'category') {
  1310. $posts = get_category($var, 1, $count);
  1311. if ($posts) $posts = $posts[0];
  1312. } elseif ($filter == 'type') {
  1313. $posts = get_type($var, 1, $count);
  1314. if ($posts) $posts = $posts[0];
  1315. } elseif ($filter == 'posts') {
  1316. $posts = get_posts(null, 1, $count);
  1317. } elseif ($filter == 'tag') {
  1318. $posts = get_tag($var, 1, $count);
  1319. if ($posts) $posts = $posts[0];
  1320. }
  1321. if (!empty($posts)) {
  1322. $tmp = serialize($posts);
  1323. file_put_contents($filename, print_r($tmp, true), LOCK_EX);
  1324. }
  1325. }
  1326. } else {
  1327. if ($filter == 'profile') {
  1328. $posts = get_profile_posts($var, 1, $count);
  1329. if ($posts) $posts = $posts[0];
  1330. } elseif ($filter == 'category') {
  1331. $posts = get_category($var, 1, $count);
  1332. if ($posts) $posts = $posts[0];
  1333. } elseif ($filter == 'type') {
  1334. $posts = get_type($var, 1, $count);
  1335. if ($posts) $posts = $posts[0];
  1336. } elseif ($filter == 'posts') {
  1337. $posts = get_posts(null, 1, $count);
  1338. } elseif ($filter == 'tag') {
  1339. $posts = get_tag($var, 1, $count);
  1340. if ($posts) $posts = $posts[0];
  1341. }
  1342. if (!empty($posts)) {
  1343. $tmp = serialize($posts);
  1344. file_put_contents($filename, print_r($tmp, true), LOCK_EX);
  1345. }
  1346. }
  1347. if (!empty($custom)) {
  1348. $arr = array();
  1349. $i = 1;
  1350. foreach ($posts as $post) {
  1351. $arr[] = $post;
  1352. if ($i++ >= $count)
  1353. break;
  1354. }
  1355. return $arr;
  1356. } else {
  1357. $i = 1;
  1358. $recent .= '<ul>';
  1359. foreach ($posts as $post) {
  1360. $recent .= '<li><a href="' . $post->url . '">' . $post->title . '</a></li>';
  1361. if ($i++ >= $count)
  1362. break;
  1363. }
  1364. if (empty($posts)) {
  1365. $recent .= '<li>' . i18n('No_posts_found') . '</li>';
  1366. }
  1367. $recent .= '</ul>';
  1368. return $recent;
  1369. }
  1370. }
  1371. // Return popular posts lists
  1372. function popular_posts($custom = null, $count = null)
  1373. {
  1374. static $_views = array();
  1375. $tmp = array();
  1376. $posts_list = get_blog_posts();
  1377. $pop = '';
  1378. if (empty($count)) {
  1379. $count = config('popular.count');
  1380. if (empty($count)) {
  1381. $count = 5;
  1382. }
  1383. }
  1384. if (config('views.counter') == 'true') {
  1385. if (empty($_views)) {
  1386. $filename = 'content/data/views.json';
  1387. if (file_exists($filename)) {
  1388. $_views = json_decode(file_get_contents($filename), true);
  1389. if(is_array($_views)) {
  1390. arsort($_views);
  1391. $i = 1;
  1392. foreach ($_views as $key => $val) {
  1393. $arr = explode('post_', $key);
  1394. if (isset($arr[1])) {
  1395. foreach($posts_list as $in => $f) {
  1396. $ex = explode('_', $f['basename']);
  1397. if ($ex[2] == $arr[1] . '.md') {
  1398. $tmp[] = $f;
  1399. if ($i++ >= $count)
  1400. break;
  1401. }
  1402. }
  1403. }
  1404. }
  1405. $dir = "cache/widget";
  1406. $filecache = "cache/widget/popular.cache";
  1407. $ar = array();
  1408. $posts = array();
  1409. if (!is_dir($dir)) {
  1410. mkdir($dir, 0775, true);
  1411. }
  1412. if (file_exists($filecache)) {
  1413. $posts = unserialize(file_get_contents($filecache));
  1414. if (count($posts) < $count) {
  1415. $posts = get_posts($tmp, 1, $count);
  1416. $ar = serialize($posts);
  1417. file_put_contents($filecache, print_r($ar, true), LOCK_EX);
  1418. }
  1419. } else {
  1420. $posts = get_posts($tmp, 1, $count);
  1421. $ar = serialize($posts);
  1422. file_put_contents($filecache, print_r($ar, true), LOCK_EX);
  1423. }
  1424. if (empty($custom)) {
  1425. $ix = 1;
  1426. $pop .= '<ul>';
  1427. foreach ($posts as $post) {
  1428. $pop .= '<li><a href="' . $post->url . '">' . $post->title . '</a></li>';
  1429. if ($ix++ >= $count)
  1430. break;
  1431. }
  1432. $pop .= '</ul>';
  1433. return $pop;
  1434. } else {
  1435. $arp = array();
  1436. $ix = 1;
  1437. foreach ($posts as $post) {
  1438. $arp[] = $post;
  1439. if ($ix++ >= $count)
  1440. break;
  1441. }
  1442. return $arp;
  1443. }
  1444. } else {
  1445. if(empty($custom)) {
  1446. $pop .= '<ul><li>No popular posts found</li></ul>';
  1447. return $pop;
  1448. } else {
  1449. return $tmp;
  1450. }
  1451. }
  1452. } else {
  1453. if (empty($custom)) {
  1454. $pop .= '<ul><li>No popular posts found</li></ul>';
  1455. return $pop;
  1456. } else {
  1457. return $tmp;
  1458. }
  1459. }
  1460. }
  1461. } else {
  1462. if (empty($custom)) {
  1463. $pop .= '<ul><li>No popular posts found</li></ul>';
  1464. return $pop;
  1465. } else {
  1466. return $tmp;
  1467. }
  1468. }
  1469. }
  1470. // Return an archive list, categorized by year and month.
  1471. function archive_list($custom = null)
  1472. {
  1473. $dir = "cache/widget";
  1474. $filename = "cache/widget/archive.cache";
  1475. $ar = array();
  1476. $arch = '';
  1477. if (!is_dir($dir)) {
  1478. mkdir($dir, 0775, true);
  1479. }
  1480. $posts = get_blog_posts();
  1481. $by_year = array();
  1482. $col = array();
  1483. if (!empty($posts)) {
  1484. if (!file_exists($filename)) {
  1485. foreach ($posts as $index => $v) {
  1486. $arr = explode('_', $v['filename']);
  1487. $date = $arr[0];
  1488. $data = explode('-', $date);
  1489. $col[] = $data;
  1490. }
  1491. foreach ($col as $row) {
  1492. $y = $row['0'];
  1493. $m = $row['1'];
  1494. $by_year[$y][] = $m;
  1495. }
  1496. $ar = serialize($by_year);
  1497. file_put_contents($filename, print_r($ar, true), LOCK_EX);
  1498. } else {
  1499. $by_year = unserialize(file_get_contents($filename));
  1500. }
  1501. # Most recent year first
  1502. krsort($by_year);
  1503. # Iterate for display
  1504. $i = 0;
  1505. $len = count($by_year);
  1506. if (empty($custom)) {
  1507. $cache_d = "cache/widget/archive.default.cache";
  1508. if (file_exists($cache_d)) {
  1509. $arch = unserialize(file_get_contents($cache_d));
  1510. return $arch;
  1511. } else {
  1512. foreach ($by_year as $year => $months) {
  1513. if ($i == 0) {
  1514. $class = 'expanded';
  1515. $arrow = '&#9660;';
  1516. } else {
  1517. $class = 'collapsed';
  1518. $arrow = '&#9658;';
  1519. }
  1520. $i++;
  1521. $by_month = array_count_values($months);
  1522. # Sort the months
  1523. krsort($by_month);
  1524. $script = <<<EOF
  1525. if (this.parentNode.className.indexOf('expanded') > -1){this.parentNode.className = 'collapsed';this.innerHTML = '&#9658;';} else {this.parentNode.className = 'expanded';this.innerHTML = '&#9660;';}
  1526. EOF;
  1527. $arch .= '<ul class="archivegroup">';
  1528. $arch .= '<li class="' . $class . '">';
  1529. $arch .= '<a href="javascript:void(0)" class="toggle" onclick="' . $script . '">' . $arrow . '</a> ';
  1530. $arch .= '<a href="' . site_url() . 'archive/' . $year . '">' . $year . '</a> ';
  1531. $arch .= '<span class="count">(' . count($months) . ')</span>';
  1532. $arch .= '<ul class="month">';
  1533. foreach ($by_month as $month => $count) {
  1534. $name = format_date(mktime(0, 0, 0, $month, 1, 2010), 'F');
  1535. $arch .= '<li class="item"><a href="' . site_url() . 'archive/' . $year . '-' . $month . '">' . $name . '</a>';
  1536. $arch .= ' <span class="count">(' . $count . ')</span></li>';
  1537. }
  1538. $arch .= '</ul>';
  1539. $arch .= '</li>';
  1540. $arch .= '</ul>';
  1541. }
  1542. $ar = serialize($arch);
  1543. file_put_contents($cache_d, $ar, LOCK_EX);
  1544. return $arch;
  1545. }
  1546. } elseif ($custom === 'month-year') {
  1547. $cache_my = "cache/widget/archive.month-year.cache";
  1548. if (file_exists($cache_my)) {
  1549. $arch = unserialize(file_get_contents($cache_my));
  1550. return $arch;
  1551. } else {
  1552. foreach ($by_year as $year => $months) {
  1553. $by_month = array_count_values($months);
  1554. # Sort the months
  1555. krsort($by_month);
  1556. foreach ($by_month as $month => $count) {
  1557. $name = format_date(mktime(0, 0, 0, $month, 1, 2010), 'F');
  1558. $arch .= '<li class="item"><a href="' . site_url() . 'archive/' . $year . '-' . $month . '">' . $name . ' ' . $year .'</a> ('.$count.')</li>';
  1559. }
  1560. }
  1561. $ar = serialize($arch);
  1562. file_put_contents($cache_my, $ar, LOCK_EX);
  1563. return $arch;
  1564. }
  1565. } elseif ($custom === 'year') {
  1566. $cache_y = "cache/widget/archive.year.cache";
  1567. if (file_exists($cache_y)) {
  1568. $arch = unserialize(file_get_contents($cache_y));
  1569. return $arch;
  1570. } else {
  1571. foreach ($by_year as $year => $months) {
  1572. $by_month = array_count_values($months);
  1573. # Sort the months
  1574. krsort($by_month);
  1575. $arch .= '<li class="item"><a href="' . site_url() . 'archive/' . $year . '">' . $year .'</a> ('. count($months) .')</li>';
  1576. }
  1577. $ar = serialize($arch);
  1578. file_put_contents($cache_y, $ar, LOCK_EX);
  1579. return $arch;
  1580. }
  1581. } else {
  1582. return $by_year;
  1583. }
  1584. }
  1585. }
  1586. // Return tag cloud.
  1587. function tag_cloud($custom = null)
  1588. {
  1589. $dir = "cache/widget";
  1590. $filename = "cache/widget/tags.cache";
  1591. $tg = array();
  1592. if (!is_dir($dir)) {
  1593. mkdir($dir, 0775, true);
  1594. }
  1595. $posts = get_blog_posts();
  1596. $tags = array();
  1597. $tagcloud_count = config('tagcloud.count');
  1598. if(empty($tagcloud_count)) {
  1599. $tagcloud_count = 40;
  1600. }
  1601. if (!empty($posts)) {
  1602. if (!file_exists($filename)) {
  1603. foreach ($posts as $index => $v) {
  1604. $arr = explode('_', $v['filename']);
  1605. $data = rtrim($arr[1], ',');
  1606. $mtag = explode(',', $data);
  1607. foreach ($mtag as $etag) {
  1608. $tags[] = strtolower($etag);
  1609. }
  1610. }
  1611. $tag_collection = array_count_values($tags);
  1612. ksort($tag_collection);
  1613. $tg = serialize($tag_collection);
  1614. file_put_contents($filename, print_r($tg, true), LOCK_EX);
  1615. } else {
  1616. $tag_collection = unserialize(file_get_contents($filename));
  1617. }
  1618. if(empty($custom)) {
  1619. $wrapper = '';
  1620. $cache_t = "cache/widget/tags.default.cache";
  1621. if (file_exists($cache_t)) {
  1622. $wrapper = unserialize(file_get_contents($cache_t));
  1623. return $wrapper;
  1624. } else {
  1625. // Font sizes
  1626. $max_size = 22; // max font size in %
  1627. $min_size = 8; // min font size in %
  1628. // Get the largest and smallest array values
  1629. $max_qty = max(array_values($tag_collection));
  1630. $min_qty = min(array_values($tag_collection));
  1631. // Find the range of values
  1632. $spread = $max_qty - $min_qty;
  1633. if (0 == $spread) { // we don't want to divide by zero
  1634. $spread = 1;
  1635. }
  1636. // Font-size increment
  1637. // this is the increase per tag quantity (times used)
  1638. $step = ($max_size - $min_size)/($spread);
  1639. arsort($tag_collection);
  1640. $sliced_tags = array_slice($tag_collection, 0, $tagcloud_count, true);
  1641. ksort($sliced_tags);
  1642. foreach ($sliced_tags as $tag => $count) {
  1643. $size = $min_size + (($count - $min_qty) * $step);
  1644. $wrapper .= ' <a class="tag-cloud-link" href="'. site_url(). 'tag/'. $tag .'" style="font-size:'. $size .'pt;">'.tag_i18n($tag).'</a> ';
  1645. }
  1646. $ar = serialize($wrapper);
  1647. file_put_contents($cache_t, $ar, LOCK_EX);
  1648. return $wrapper;
  1649. }
  1650. } else {
  1651. return $tag_collection;
  1652. }
  1653. } else {
  1654. if(empty($custom)) return;
  1655. return $tags;
  1656. }
  1657. }
  1658. // Helper function to determine whether
  1659. // to show the previous buttons
  1660. function has_prev($prev)
  1661. {
  1662. if (!empty($prev)) {
  1663. return array(
  1664. 'url' => $prev->url,
  1665. 'title' => $prev->title,
  1666. 'date' => $prev->date,
  1667. 'body' => $prev->body,
  1668. 'description' => $prev->description,
  1669. 'tag' => $prev->tag,
  1670. 'author' => $prev->author,
  1671. 'authorName' => $prev->authorName,
  1672. 'authorDescription' => $prev->authorDescription,
  1673. 'authorAbout' => $prev->authorAbout,
  1674. 'authorUrl' => $prev->authorUrl,
  1675. 'authorAvatar' => $prev->authorAvatar,
  1676. 'authorRss' => $prev->authorRss,
  1677. 'related' => $prev->related,
  1678. 'views' => $prev->views,
  1679. 'type' => $prev->type,
  1680. 'file' => $prev->file,
  1681. 'image' => $prev->image,
  1682. 'video' => $prev->video,
  1683. 'audio' => $prev->audio,
  1684. 'quote' => $prev->quote,
  1685. 'link' => $prev->link,
  1686. 'slug' => $prev->slug,
  1687. 'category' => $prev->category,
  1688. 'categoryUrl' => $prev->categoryUrl,
  1689. 'categoryCount' => $prev->categoryCount,
  1690. 'categorySlug' => $prev->categorySlug,
  1691. 'categoryMd' => $prev->categoryMd,
  1692. 'categoryTitle' => $prev->categoryTitle,
  1693. 'readTime' => $prev->readTime,
  1694. 'lastMod' => $prev->lastMod,
  1695. 'raw' => $prev->raw
  1696. );
  1697. }
  1698. }
  1699. // Helper function to determine whether
  1700. // to show the next buttons
  1701. function has_next($next)
  1702. {
  1703. if (!empty($next)) {
  1704. return array(
  1705. 'url' => $next->url,
  1706. 'title' => $next->title,
  1707. 'date' => $next->date,
  1708. 'body' => $next->body,
  1709. 'description' => $next->description,
  1710. 'tag' => $next->tag,
  1711. 'author' => $next->author,
  1712. 'authorName' => $next->authorName,
  1713. 'authorDescription' => $next->authorDescription,
  1714. 'authorAbout' => $next->authorAbout,
  1715. 'authorUrl' => $next->authorUrl,
  1716. 'authorAvatar' => $next->authorAvatar,
  1717. 'authorRss' => $next->authorRss,
  1718. 'related' => $next->related,
  1719. 'views' => $next->views,
  1720. 'type' => $next->type,
  1721. 'file' => $next->file,
  1722. 'image' => $next->image,
  1723. 'video' => $next->video,
  1724. 'audio' => $next->audio,
  1725. 'quote' => $next->quote,
  1726. 'link' => $next->link,
  1727. 'slug' => $next->slug,
  1728. 'category' => $next->category,
  1729. 'categoryUrl' => $next->categoryUrl,
  1730. 'categoryCount' => $next->categoryCount,
  1731. 'categorySlug' => $next->categorySlug,
  1732. 'categoryMd' => $next->categoryMd,
  1733. 'categoryTitle' => $next->categoryTitle,
  1734. 'readTime' => $next->readTime,
  1735. 'lastMod' => $next->lastMod,
  1736. 'raw' => $next->raw
  1737. );
  1738. }
  1739. }
  1740. function static_prev($prev)
  1741. {
  1742. if (!empty($prev)) {
  1743. return array(
  1744. 'url' => $prev->url,
  1745. 'title' => $prev->title,
  1746. 'body' => $prev->body,
  1747. 'description' => $prev->description,
  1748. 'views' => $prev->views,
  1749. 'md' => $prev->md,
  1750. 'slug' => $prev->slug,
  1751. 'parent' => $prev->parent,
  1752. 'parentSlug' => $prev->parentSlug,
  1753. 'file' => $prev->file,
  1754. 'readTime' => $prev->readTime,
  1755. 'lastMod' => $prev->lastMod,
  1756. 'raw' => $prev->raw
  1757. );
  1758. }
  1759. }
  1760. function static_next($next)
  1761. {
  1762. if (!empty($next)) {
  1763. return array(
  1764. 'url' => $next->url,
  1765. 'title' => $next->title,
  1766. 'body' => $next->body,
  1767. 'description' => $next->description,
  1768. 'views' => $next->views,
  1769. 'md' => $next->md,
  1770. 'slug' => $next->slug,
  1771. 'parent' => $next->parent,
  1772. 'parentSlug' => $next->parentSlug,
  1773. 'file' => $next->file,
  1774. 'readTime' => $next->readTime,
  1775. 'lastMod' => $next->lastMod,
  1776. 'raw' => $next->raw
  1777. );
  1778. }
  1779. }
  1780. // Helper function to determine whether
  1781. // to show the pagination buttons
  1782. function has_pagination($total, $perpage, $page = 1)
  1783. {
  1784. if (!$total) {
  1785. return false;
  1786. }
  1787. $totalPage = ceil($total / $perpage);
  1788. $number = i18n('Page') . ' ' . $page . ' ' . i18n('of') . ' ' . $totalPage;
  1789. $pager = get_pagination($total, $page, $perpage, 2);
  1790. return array(
  1791. 'prev' => $page > 1,
  1792. 'next' => $total > $page * $perpage,
  1793. 'pagenum' => $number,
  1794. 'html' => $pager,
  1795. 'items' => $total,
  1796. 'perpage' => $perpage
  1797. );
  1798. }
  1799. //function to return the pagination string
  1800. function get_pagination($totalitems, $page = 1, $perpage = 10, $adjacents = 1, $pagestring = '?page=')
  1801. {
  1802. //defaults
  1803. if(!$adjacents) $adjacents = 1;
  1804. if(!$perpage) $perpage = 10;
  1805. if(!$page) $page = 1;
  1806. //other vars
  1807. $prev = $page - 1; //previous page is page - 1
  1808. $next = $page + 1; //next page is page + 1
  1809. $lastpage = ceil($totalitems / $perpage); //lastpage is = total items / items per page, rounded up.
  1810. $lpm1 = $lastpage - 1; //last page minus 1
  1811. /*
  1812. Now we apply our rules and draw the pagination object.
  1813. We're actually saving the code to a variable in case we want to draw it more than once.
  1814. */
  1815. $pagination = '';
  1816. $curpage = strtok($_SERVER["REQUEST_URI"], '?');
  1817. if($lastpage > 1)
  1818. {
  1819. $pagination .= '<ul class="pagination">';
  1820. //newer button
  1821. if ($page > 2)
  1822. $pagination .= '<li class="page-item"><a class="page-link" href="'. $pagestring . $prev .'">« '. i18n('Newer') .'</a></li>';
  1823. else if ($page == 2)
  1824. $pagination .= '<li class="page-item"><a class="page-link" href="'. $curpage .'">« '. i18n('Newer') .'</a></li>';
  1825. else
  1826. $pagination .= '<li class="page-item disabled"><span class="page-link">« '. i18n('Newer') . '</span></li>';
  1827. //pages
  1828. if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
  1829. {
  1830. for ($counter = 1; $counter <= $lastpage; $counter++)
  1831. {
  1832. if ($counter == 1 && $counter !== $page) // link 1st pagination page to parent page instead of ?page=1 for SEO
  1833. $pagination .= '<li class="page-item"><a class="page-link" href="'. $curpage .'">1</a></li>';
  1834. else if ($counter == $page)
  1835. $pagination .= '<li class="page-item active"><span class="page-link">'. $counter.'</span></li>';
  1836. else
  1837. $pagination .= '<li class="page-item"><a class="page-link" href="'. $curpage . $pagestring . $counter .'">'. $counter .'</a></li>';
  1838. }
  1839. }
  1840. elseif($lastpage >= 7 + ($adjacents * 2)) //enough pages to hide some
  1841. {
  1842. //close to beginning; only hide later pages
  1843. if($page < 1 + ($adjacents * 3))
  1844. {
  1845. for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
  1846. {
  1847. if ($counter == 1 && $counter !== $page) // link 1st pagination page to parent page instead of ?page=1 for SEO
  1848. $pagination .= '<li class="page-item"><a class="page-link" href="'. $curpage .'">1</a></li>';
  1849. else if ($counter == $page)
  1850. $pagination .= '<li class="page-item active"><span class="page-link">'. $counter .'</span></li>';
  1851. else
  1852. $pagination .= '<li class="page-item"><a class="page-link" href="'. $curpage . $pagestring . $counter .'">'. $counter .'</a></li>';
  1853. }
  1854. $pagination .= '<li class="page-item disabled"><span class="page-link">...</span></li>';
  1855. $pagination .= '<li class="page-item"><a class="page-link" href="'. $curpage . $pagestring . $lpm1 .'">'. $lpm1 .'</a></li>';
  1856. $pagination .= '<li class="page-item"><a class="page-link" href="'. $curpage . $pagestring . $lastpage .'">'. $lastpage .'</a></li>';
  1857. }
  1858. //in middle; hide some front and some back
  1859. elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
  1860. {
  1861. $pagination .= '<li class="page-item"><a class="page-link" href="' . $curpage .'">1</a></li>';
  1862. $pagination .= '<li class="page-item"><a class="page-link" href="'. $pagestring .'2">2</a></li>';
  1863. $pagination .= '<li class="page-item disabled"><span class="page-link">...</span></li>';
  1864. for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
  1865. {
  1866. if ($counter == $page)
  1867. $pagination .= '<li class="page-item active"><span class="page-link">'. $counter .'</span></li>';
  1868. else
  1869. $pagination .= '<li class="page-item"><a class="page-link" href="'. $curpage . $pagestring . $counter .'">'. $counter .'</a></li>';
  1870. }
  1871. $pagination .= '<li class="page-item disabled"><span class="page-link">...</span></li>';
  1872. $pagination .= '<li class="page-item"><a class="page-link" href="'. $curpage . $pagestring . $lpm1 .'">'. $lpm1 .'</a></li>';
  1873. $pagination .= '<li class="page-item"><a class="page-link" href="'. $curpage . $pagestring . $lastpage . '">'. $lastpage .'</a></li>';
  1874. }
  1875. //close to end; only hide early pages
  1876. else
  1877. {
  1878. $pagination .= '<li class="page-item"><a class="page-link" href="'. $curpage .'">1</a></li>';
  1879. $pagination .= '<li class="page-item"><a class="page-link" href="'. $pagestring .'2">2</a></li>';
  1880. $pagination .= '<li class="page-item disabled"><span class="page-link">...</span></li>';
  1881. for ($counter = $lastpage - (1 + ($adjacents * 3)); $counter <= $lastpage; $counter++)
  1882. {
  1883. if ($counter == $page)
  1884. $pagination .= '<li class="page-item active"><span class="page-link">'. $counter .'</span></li>';
  1885. else
  1886. $pagination .= '<li class="page-item"><a class="page-link" href="'. $curpage . $pagestring . $counter .'">'. $counter .'</a></li>';
  1887. }
  1888. }
  1889. }
  1890. //older button
  1891. if ($page < $counter - 1)
  1892. $pagination .= '<li class="page-item"><a class="page-link" href="'. $curpage . $pagestring . $next .'">'. i18n('Older') .' »</a></li>';
  1893. else
  1894. $pagination .= '<li class="page-item disabled"><span class="page-link">'. i18n('Older') .' »</span></li>';
  1895. $pagination .= '</ul>';
  1896. }
  1897. return $pagination;
  1898. }
  1899. // Get the meta description
  1900. function get_description($string, $char = null)
  1901. {
  1902. if(empty($char)) {
  1903. $char = config('description.char');
  1904. if(empty($char)) {
  1905. $char = 150;
  1906. }
  1907. }
  1908. if (strlen(strip_tags($string)) < $char) {
  1909. return safe_html(strip_tags($string));
  1910. } else {
  1911. $string = safe_html(strip_tags($string));
  1912. $string = substr($string, 0, $char);
  1913. $string = substr($string, 0, strrpos($string, ' '));
  1914. return $string;
  1915. }
  1916. }
  1917. // Get the teaser
  1918. function get_teaser($string, $url = null, $char = null)
  1919. {
  1920. $teaserType = config('teaser.type');
  1921. $more = config('read.more');
  1922. $behave = config('teaser.behave');
  1923. if(empty($more)) {
  1924. $more = 'Read more';
  1925. }
  1926. if(empty($char)) {
  1927. $char = config('teaser.char');
  1928. if(empty($char)) {
  1929. $char = 200;
  1930. }
  1931. }
  1932. if ($teaserType === 'full') {
  1933. $readMore = explode('<!--more-->', $string);
  1934. if (isset($readMore['1'])) {
  1935. $string = replace_href($readMore['0'], 'a', 'footnote-ref', $url);
  1936. return $string . '<div class="jump-link"><a class="read-more btn btn-cta-secondary" href="'. $url .'#more">' . $more . '</a></div>';
  1937. } else {
  1938. return $string;
  1939. }
  1940. } else {
  1941. if ($behave === 'check') {
  1942. $readMore = explode('<!--more-->', $string);
  1943. if (isset($readMore['1'])) {
  1944. $string = shorten($readMore[0]);
  1945. return $string;
  1946. } else {
  1947. $string = shorten($string, $char);
  1948. return $string;
  1949. }
  1950. } else {
  1951. $string = shorten($string, $char);
  1952. return $string;
  1953. }
  1954. }
  1955. }
  1956. // Shorten the string
  1957. function shorten($string = null, $char = null)
  1958. {
  1959. if(empty($string)) {
  1960. return;
  1961. }
  1962. $string = str_replace('<span class="details">'. config('toc.label') .'</span>', '', $string);
  1963. libxml_use_internal_errors(true);
  1964. $dom = new DOMDocument();
  1965. $dom->loadHTML('<span class="dom-charset"><meta charset="utf8"></span>' . $string);
  1966. $tags_to_remove = array('script', 'style');
  1967. foreach($tags_to_remove as $tag){
  1968. $element = $dom->getElementsByTagName($tag);
  1969. foreach($element as $item){
  1970. $item->parentNode->removeChild($item);
  1971. }
  1972. }
  1973. $string = preg_replace('~<(?:!DOCTYPE|/?(?:html|head|body))[^>]*>\s*~i', '', mb_convert_encoding($dom->saveHTML($dom->documentElement), 'UTF-8'));
  1974. $string = preg_replace('/\s\s+/', ' ', strip_tags($string));
  1975. $string = ltrim(rtrim($string));
  1976. $string = str_replace('<span class="dom-charset"><meta charset="utf8"></span>', '', $string);
  1977. if (!empty($char)) {
  1978. if (mb_strlen($string) > $char) {
  1979. $string = mb_substr($string, 0, $char);
  1980. $string = mb_substr($string, 0, mb_strrpos($string, ' '));
  1981. }
  1982. }
  1983. return $string;
  1984. }
  1985. // Get thumbnail from image and Youtube.
  1986. function get_thumbnail($text, $url = null)
  1987. {
  1988. if (config('img.thumbnail') == 'true') {
  1989. $teaserType = config('teaser.type');
  1990. if (strlen(strip_tags($text)) > config('teaser.char') && $teaserType === 'trimmed') {
  1991. libxml_use_internal_errors(true);
  1992. $default = config('default.thumbnail');
  1993. $dom = new DOMDocument();
  1994. $dom->loadHtml('<meta charset="utf8">' . $text);
  1995. $imgTags = $dom->getElementsByTagName('img');
  1996. $vidTags = $dom->getElementsByTagName('iframe');
  1997. if ($imgTags->length > 0) {
  1998. $imgElement = $imgTags->item(0);
  1999. $imgSource = $imgElement->getAttribute('src');
  2000. if (!empty($url)) {
  2001. return $imgSource;
  2002. } else {
  2003. return '<div class="thumbnail" style="background-image:url(' . $imgSource . ');"></div>';
  2004. }
  2005. } elseif ($vidTags->length > 0) {
  2006. $vidElement = $vidTags->item(0);
  2007. $vidSource = $vidElement->getAttribute('src');
  2008. $fetch = explode("embed/", $vidSource);
  2009. if (isset($fetch[1])) {
  2010. $vidThumb = '//img.youtube.com/vi/' . $fetch[1] . '/default.jpg';
  2011. if (!empty($url)) {
  2012. return $vidThumb;
  2013. } else {
  2014. return '<div class="thumbnail" style="background-image:url(' . $vidThumb . ');"></div>';
  2015. }
  2016. }
  2017. } else {
  2018. if (!empty($default)) {
  2019. if (!empty($url)) {
  2020. return $default;
  2021. } else {
  2022. return '<div class="thumbnail" style="background-image:url(' . $default . ');"></div>';
  2023. }
  2024. }
  2025. }
  2026. } else {
  2027. // Ignore
  2028. }
  2029. }
  2030. }
  2031. // Get image from post and Youtube thumbnail.
  2032. function get_image($text, $width = null, $height = null)
  2033. {
  2034. libxml_use_internal_errors(true);
  2035. $dom = new DOMDocument();
  2036. $dom->loadHtml('<meta charset="utf8">' . $text);
  2037. $imgTags = $dom->getElementsByTagName('img');
  2038. $vidTags = $dom->getElementsByTagName('iframe');
  2039. if ($imgTags->length > 0) {
  2040. $imgElement = $imgTags->item(0);
  2041. $imgSource = $imgElement->getAttribute('src');
  2042. if(is_null($width)) {
  2043. return $imgSource;
  2044. } else {
  2045. return create_thumb($imgSource, $width, $height);
  2046. }
  2047. } elseif ($vidTags->length > 0) {
  2048. $vidElement = $vidTags->item(0);
  2049. $vidSource = $vidElement->getAttribute('src');
  2050. $fetch = explode("embed/", $vidSource);
  2051. if (isset($fetch[1])) {
  2052. $vidThumb = '//img.youtube.com/vi/' . $fetch[1] . '/sddefault.jpg';
  2053. return $vidThumb;
  2054. }
  2055. } else{
  2056. return false;
  2057. }
  2058. }
  2059. // Return edit tab on post
  2060. function tab($p)
  2061. {
  2062. $user = $_SESSION[site_url()]['user'];
  2063. $role = user('role', $user);
  2064. $tab = '';
  2065. if (isset($p->author)) {
  2066. if ($user === $p->author || $role === 'editor' || $role === 'admin') {
  2067. $tab = '<div class="tab"><ul class="nav nav-tabs"><li role="presentation" class="active"><a href="' . $p->url . '">' . i18n('View') .'</a></li><li><a href="' . $p->url . '/edit?destination=post">'. i18n('Edit') .'</a></li></ul></div>';
  2068. }
  2069. } else {
  2070. if ($p->url) {
  2071. if ($role === 'editor' || $role === 'admin') {
  2072. $tab = '<div class="tab"><ul class="nav nav-tabs"><li role="presentation" class="active"><a href="' . $p->url . '">' . i18n('View') .'</a></li><li><a href="' . $p->url . '/edit?destination=post">'. i18n('Edit') .'</a></li></ul></div>';
  2073. }
  2074. }
  2075. }
  2076. return $tab;
  2077. }
  2078. // Social links
  2079. function social($class = null)
  2080. {
  2081. $bluesky = config('social.bluesky');
  2082. $twitter = config('social.twitter');
  2083. $facebook = config('social.facebook');
  2084. $instagram = config('social.instagram');
  2085. $linkedin = config('social.linkedin');
  2086. $github = config('social.github');
  2087. $youtube = config('social.youtube');
  2088. $mastodon = config('social.mastodon');
  2089. $tiktok = config('social.tiktok');
  2090. $rss = site_url() . 'feed/rss';
  2091. $social = '';
  2092. $social .= '<div class="social-logo ' . $class . '">';
  2093. $social .= '<link rel="stylesheet" id="social-logo-style" href="'. site_url() .'system/resources/css/social-logos.css" type="text/css" media="all">';
  2094. if (!empty($bluesky)) {
  2095. $social .= '<a class="social-logo-bluesky" href="' . $bluesky . '" target="_blank" rel="nofollow"><span class="screen-reader-text">Bluesky</span></a>';
  2096. }
  2097. if (!empty($twitter)) {
  2098. $social .= '<a class="social-logo-x" href="' . $twitter . '" target="_blank" rel="nofollow"><span class="screen-reader-text">Twitter</span></a>';
  2099. }
  2100. if (!empty($facebook)) {
  2101. $social .= '<a class="social-logo-facebook" href="' . $facebook . '" target="_blank" rel="nofollow"><span class="screen-reader-text">Facebook</span></a>';
  2102. }
  2103. if (!empty($instagram)) {
  2104. $social .= '<a class="social-logo-instagram" href="' . $instagram . '" target="_blank" rel="nofollow"><span class="screen-reader-text">Instagram</span></a>';
  2105. }
  2106. if (!empty($linkedin)) {
  2107. $social .= '<a class="social-logo-linkedin" href="' . $linkedin . '" target="_blank" rel="nofollow"><span class="screen-reader-text">Linkedin</span></a>';
  2108. }
  2109. if (!empty($github)) {
  2110. $social .= '<a class="social-logo-github" href="' . $github . '" target="_blank" rel="nofollow"><span class="screen-reader-text">Github</span></a>';
  2111. }
  2112. if (!empty($youtube)) {
  2113. $social .= '<a class="social-logo-youtube" href="' . $youtube . '" target="_blank" rel="nofollow"><span class="screen-reader-text">Youtube</span></a>';
  2114. }
  2115. if (!empty($mastodon)) {
  2116. $social .= '<a class="social-logo-mastodon" href="' . $mastodon . '" target="_blank" rel="nofollow"><span class="screen-reader-text">Mastodon</span></a>';
  2117. }
  2118. if (!empty($tiktok)) {
  2119. $social .= '<a class="social-logo-tiktok" href="' . $tiktok . '" target="_blank" rel="nofollow"><span class="screen-reader-text">TikTok</span></a>';
  2120. }
  2121. $social .= '<a class="social-logo-feed" href="' . $rss . '" target="_blank"><span class="screen-reader-text">RSS</span></a>';
  2122. $social .= '</div>';
  2123. return $social;
  2124. }
  2125. // Copyright
  2126. function copyright()
  2127. {
  2128. $blogcp = blog_copyright();
  2129. $credit = i18n('proudly_powered_by') . ' ' . '<a href="http://www.htmly.com" target="_blank" rel="nofollow">HTMLy</a>';
  2130. if (!empty($blogcp)) {
  2131. return $copyright = '<span class="copyright">' . $blogcp . '</span> <span class="credit">' . $credit . '</span>';
  2132. } else {
  2133. return $credit = '<span class="credit">' . $credit . '</span>';
  2134. }
  2135. }
  2136. // Disqus on post.
  2137. function disqus($title = null, $url = null)
  2138. {
  2139. $comment = config('comment.system');
  2140. $disqus = config('disqus.shortname');
  2141. $script = <<<EOF
  2142. <script type="text/javascript">
  2143. var getAbsolutePath = function(href) {
  2144. var link = document.createElement('a');
  2145. link.href = href;
  2146. return link.href;
  2147. };
  2148. var disqus_shortname = '{$disqus}';
  2149. var disqus_title = '{$title}';
  2150. var disqus_url = getAbsolutePath('{$url}');
  2151. (function () {
  2152. var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
  2153. dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
  2154. (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  2155. })();
  2156. </script>
  2157. EOF;
  2158. if (!empty($disqus) && $comment == 'disqus') {
  2159. return $script;
  2160. }
  2161. }
  2162. // Disqus comment count on teaser
  2163. function disqus_count()
  2164. {
  2165. $comment = config('comment.system');
  2166. $disqus = config('disqus.shortname');
  2167. $script = <<<EOF
  2168. <script type="text/javascript">
  2169. var disqus_shortname = '{$disqus}';
  2170. (function () {
  2171. var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
  2172. dsq.src = '//' + disqus_shortname + '.disqus.com/count.js';
  2173. (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  2174. })();
  2175. </script>
  2176. EOF;
  2177. if (!empty($disqus) && $comment == 'disqus') {
  2178. return $script;
  2179. }
  2180. }
  2181. // Disqus recent comments
  2182. function recent_comments()
  2183. {
  2184. $comment = config('comment.system');
  2185. $disqus = config('disqus.shortname');
  2186. $script = <<<EOF
  2187. <script type="text/javascript" src="//{$disqus}.disqus.com/recent_comments_widget.js?num_items=5&hide_avatars=0&avatar_size=48&excerpt_length=200&hide_mods=0"></script>
  2188. EOF;
  2189. if (!empty($disqus) && $comment == 'disqus') {
  2190. return $script;
  2191. }
  2192. }
  2193. // Facebook comments
  2194. function facebook()
  2195. {
  2196. $comment = config('comment.system');
  2197. $appid = config('fb.appid');
  2198. $lang = locale_language();
  2199. $script = <<<EOF
  2200. <div id="fb-root"></div>
  2201. <script>(function (d, s, id) {
  2202. var js, fjs = d.getElementsByTagName(s)[0];
  2203. if (d.getElementById(id)) return;
  2204. js = d.createElement(s);
  2205. js.id = id;
  2206. js.src = "//connect.facebook.net/{$lang}/all.js#xfbml=1&appId={$appid}";
  2207. fjs.parentNode.insertBefore(js, fjs);
  2208. }(document, 'script', 'facebook-jssdk'));
  2209. </script>
  2210. <style>.fb-comments, .fb_iframe_widget span, .fb-comments iframe {width: 100%!important;}</style>
  2211. EOF;
  2212. if (!empty($appid) && $comment == 'facebook') {
  2213. return $script;
  2214. }
  2215. }
  2216. // Google Publisher (Google+ page).
  2217. function publisher()
  2218. {
  2219. $publisher = config('google.publisher');
  2220. if (!empty($publisher)) {
  2221. return $publisher;
  2222. }
  2223. }
  2224. // Google Analytics
  2225. function analytics()
  2226. {
  2227. $analytics = config('google.analytics.id');
  2228. $gtag = config('google.gtag.id');
  2229. $script = <<<EOF
  2230. <script>
  2231. (function (i,s,o,g,r,a,m) {i['GoogleAnalyticsObject']=r;i[r]=i[r]||function () {
  2232. (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  2233. m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  2234. })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  2235. ga('create', '{$analytics}', 'auto');
  2236. ga('send', 'pageview');
  2237. </script>
  2238. EOF;
  2239. $gtagScript = <<<EOF
  2240. <!-- Global site tag (gtag.js) - Google Analytics -->
  2241. <script async src="https://www.googletagmanager.com/gtag/js?id={$gtag}"></script>
  2242. <script>
  2243. window.dataLayer = window.dataLayer || [];
  2244. function gtag(){dataLayer.push(arguments);}
  2245. gtag('js', new Date());
  2246. gtag('config', '{$gtag}');
  2247. </script>
  2248. EOF;
  2249. if (!empty($gtag)) {
  2250. return $gtagScript;
  2251. } elseif (!empty($analytics)) {
  2252. return $script;
  2253. }
  2254. }
  2255. function slashUrl($url)
  2256. {
  2257. return rtrim($url, '/') . '/';
  2258. }
  2259. function parseNodes($nodes, $child = null, $class = null)
  2260. {
  2261. if (empty($child)) {
  2262. $ul = '<ul class="nav navbar-nav '.$class.'">';
  2263. foreach ($nodes as $node) {
  2264. if (isset($node->children)) {
  2265. $ul .= parseNode($node, true);
  2266. } else {
  2267. $ul .= parseNode($node);
  2268. }
  2269. }
  2270. $ul .= '</ul>';
  2271. return $ul;
  2272. } else {
  2273. $ul = '<ul class="subnav dropdown-menu" role="menu">';
  2274. foreach ($nodes as $node) {
  2275. if (isset($node->children)) {
  2276. $ul .= parseNode($node, true);
  2277. } else {
  2278. $ul .= parseNode($node);
  2279. }
  2280. }
  2281. $ul .= '</ul>';
  2282. return $ul;
  2283. }
  2284. }
  2285. function parseNode($node, $child = null)
  2286. {
  2287. $req = strtok($_SERVER["REQUEST_URI"],'?');
  2288. $url = parse_url(slashUrl($node->slug));
  2289. $su = parse_url(site_url());
  2290. $target = !empty($node->target) ? 'target="'.$node->target.'"' : null;
  2291. if (empty($child)) {
  2292. if (isset($url['host']) && isset($su['host'])) {
  2293. if ($url['host'] == $su['host']) {
  2294. if (slashUrl($url['path']) == slashUrl($req)) {
  2295. $li = '<li class="item nav-item active '.$node->class.'">';
  2296. } else {
  2297. $li = '<li class="item nav-item '.$node->class.'">';
  2298. }
  2299. } else {
  2300. $li = '<li class="item nav-item '.$node->class.'">'; // Link out
  2301. }
  2302. } else {
  2303. if (slashUrl($node->slug) == slashUrl($req)) {
  2304. $li = '<li class="item nav-item active '.$node->class.'">';
  2305. } else {
  2306. $li = '<li class="item nav-item '.$node->class.'">';
  2307. }
  2308. }
  2309. $li .= '<a class="nav-link" href="'.htmlspecialchars(slashUrl($node->slug), FILTER_SANITIZE_URL).'" '.$target.'>'.$node->name.'</a>';
  2310. if (isset($node->children)) {
  2311. $li .= parseNodes($node->children, true, null);
  2312. }
  2313. $li .= '</li>';
  2314. return $li;
  2315. } else {
  2316. if (isset($url['host']) && isset($su['host'])) {
  2317. if ($url['host'] == $su['host']) {
  2318. if (slashUrl($url['path']) == slashUrl($req)) {
  2319. $li = '<li class="item nav-item dropdown active '.$node->class.'">';
  2320. } else {
  2321. $li = '<li class="item nav-item dropdown '.$node->class.'">';
  2322. }
  2323. } else {
  2324. $li = '<li class="item nav-item dropdown '.$node->class.'">'; // Link out
  2325. }
  2326. } else {
  2327. if (slashUrl($node->slug) == slashUrl($req)) {
  2328. $li = '<li class="item nav-item dropdown active '.$node->class.'">';
  2329. } else {
  2330. $li = '<li class="item nav-item dropdown '.$node->class.'">';
  2331. }
  2332. }
  2333. $li .= '<a class="nav-link dropdown-toggle" data-toggle="dropdown" data-bs-toggle="dropdown" href="'.htmlspecialchars(slashUrl($node->slug), FILTER_SANITIZE_URL).'" '.$target.'>'.$node->name.'<b class="caret"></b></a>';
  2334. if (isset($node->children)) {
  2335. $li .= parseNodes($node->children, true, null);
  2336. }
  2337. $li .= '</li>';
  2338. return $li;
  2339. }
  2340. }
  2341. // Menu
  2342. function menu($class = null)
  2343. {
  2344. $filename = "content/data/menu.json";
  2345. if (file_exists($filename)) {
  2346. $json = json_decode(file_get_contents('content/data/menu.json', true));
  2347. $nodes = json_decode($json);
  2348. if (empty($nodes)) {
  2349. return get_menu($class);
  2350. } else {
  2351. $html = parseNodes($nodes, null, $class);
  2352. $output = '';
  2353. libxml_use_internal_errors(true);
  2354. $doc = new DOMDocument();
  2355. $doc->loadHTML('<span class="dom-charset"><meta charset="utf8"></span>' . $html);
  2356. $finder = new DOMXPath($doc);
  2357. $elements = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' dropdown-menu ')]");
  2358. // loop through all <ul> with dropdown-menu class
  2359. foreach ($elements as $element) {
  2360. $nodes = $element->childNodes;
  2361. foreach ($nodes as $node) {
  2362. $class = $node->getAttribute('class');
  2363. if (stripos($class, 'active')) {
  2364. $parentClass = $element->parentNode->getAttribute('class') . ' active';
  2365. $element->parentNode->setAttribute('class', $parentClass);
  2366. }
  2367. }
  2368. }
  2369. $output = preg_replace('~<(?:!DOCTYPE|/?(?:html|head|body))[^>]*>\s*~i', '', mb_convert_encoding($doc->saveHTML($doc->documentElement), 'UTF-8'));
  2370. return str_replace('<span class="dom-charset"><meta charset="utf8"></span>', '', $output);
  2371. }
  2372. } else {
  2373. return get_menu($class);
  2374. }
  2375. }
  2376. // Get the title from file
  2377. function get_title_from_file($v)
  2378. {
  2379. // Get the contents and convert it to HTML
  2380. $content = MarkdownExtra::defaultTransform(file_get_contents($v));
  2381. $filename= pathinfo($v, PATHINFO_FILENAME);
  2382. // Extract the title and body
  2383. return get_content_tag('t', $content, str_replace('-', ' ', $filename));
  2384. }
  2385. // Auto generate menu from static page
  2386. function get_menu($custom = null, $auto = null)
  2387. {
  2388. $posts = get_static_pages();
  2389. $req = $_SERVER['REQUEST_URI'];
  2390. $menu = '';
  2391. if (!empty($posts)) {
  2392. $menu .= '<ul class="nav ' . $custom . '">';
  2393. if (is_null($auto)) {
  2394. if ($req == site_path() . '/' || stripos($req, site_path() . '/?page') !== false) {
  2395. $menu .= '<li class="item nav-item first active"><a class="nav-link" href="' . site_url() . '">' . config('breadcrumb.home') . '</a></li>';
  2396. } else {
  2397. $menu .= '<li class="item nav-item first"><a class="nav-link" href="' . site_url() . '">' . config('breadcrumb.home') . '</a></li>';
  2398. }
  2399. if (config('blog.enable') == 'true' ) {
  2400. if ($req == site_path() . '/' . blog_path() || stripos($req, site_path() . '/' . blog_path() . '?page') !== false) {
  2401. $menu .= '<li class="item nav-item active"><a class="nav-link" href="' . site_url() . blog_path() . '">' . blog_string() . '</a></li>';
  2402. } else {
  2403. $menu .= '<li class="item nav-item"><a class="nav-link" href="' . site_url() . blog_path() . '">' . blog_string() . '</a></li>';
  2404. }
  2405. }
  2406. }
  2407. $i = 0;
  2408. $len = count($posts);
  2409. foreach ($posts as $index => $v) {
  2410. if ($i == $len - 1) {
  2411. $class = 'item nav-item last';
  2412. } else {
  2413. $class = 'item nav-item';
  2414. }
  2415. $i++;
  2416. // Filename string
  2417. $fn = explode('.', $v['filename']);
  2418. if (isset($fn[1])) {
  2419. $filename= $fn[1];
  2420. } else {
  2421. $filename= $v['filename'];
  2422. }
  2423. $url = site_url() . $filename;
  2424. $parent_file = $v['dirname'] . '/' . $v['basename'];
  2425. $title = get_title_from_file($parent_file);
  2426. if ($req == site_path() . "/" . $filename || stripos($req, site_path() . "/" . $filename) !== false) {
  2427. $active = ' active';
  2428. $reqBase = '';
  2429. } else {
  2430. $active = '';
  2431. }
  2432. $subPages = get_static_subpages($filename);
  2433. if (!empty($subPages)) {
  2434. $menu .= '<li class="' . $class . $active .' dropdown">';
  2435. $menu .= '<a class="nav-link dropdown-toggle" data-toggle="dropdown" data-bs-toggle="dropdown" href="' . $url . '">' . ucwords($title) . '<b class="caret"></b></a>';
  2436. $menu .= '<ul class="subnav dropdown-menu" role="menu">';
  2437. $iSub = 0;
  2438. $countSub = count($subPages);
  2439. foreach ($subPages as $index => $sp) {
  2440. $classSub = "item nav-item";
  2441. if ($iSub == 0) {
  2442. $classSub .= " first";
  2443. }
  2444. if ($iSub == $countSub - 1) {
  2445. $classSub .= " last";
  2446. }
  2447. $bs = explode('.', $sp['filename']);
  2448. if (isset($bs[1])) {
  2449. $baseSub = $bs[1];
  2450. } else {
  2451. $baseSub= $sp['filename'];
  2452. }
  2453. $child_file = $sp['dirname'] . '/' . $sp['basename'];
  2454. if ($req == site_path() . "/" . $filename . "/" . $baseSub) {
  2455. $classSub .= ' active';
  2456. }
  2457. $urlSub = $url . "/" . $baseSub;
  2458. $menu .= '<li class="' . $classSub . '"><a class="nav-link" href="' . $urlSub . '">' . get_title_from_file($child_file) . '</a></li>';
  2459. $iSub++;
  2460. }
  2461. $menu .= '</ul>';
  2462. } else {
  2463. $menu .= '<li class="' . $class . $active .'">';
  2464. $menu .= '<a class="nav-link" href="' . $url . '">' . ucwords($title) . '</a>';
  2465. }
  2466. $menu .= '</li>';
  2467. }
  2468. $menu .='</ul>';
  2469. return $menu;
  2470. } else {
  2471. $menu .= '<ul class="nav ' . $custom . '">';
  2472. if ($req == site_path() . '/') {
  2473. $menu .= '<li class="item nav-item first active"><a class="nav-link" href="' . site_url() . '">' . config('breadcrumb.home') . '</a></li>';
  2474. } else {
  2475. $menu .= '<li class="item nav-item first"><a class="nav-link" href="' . site_url() . '">' . config('breadcrumb.home') . '</a></li>';
  2476. }
  2477. if (config('blog.enable') == 'true' ) {
  2478. if ($req == site_path() . '/' . blog_path() || stripos($req, site_path() . '/'. blog_path() .'?page') !== false) {
  2479. $menu .= '<li class="item nav-item active"><a class="nav-link" href="' . site_url() . blog_path() . '">' . blog_string() . '</a></li>';
  2480. } else {
  2481. $menu .= '<li class="item nav-item"><a class="nav-link" href="' . site_url() . blog_path() . '">' . blog_string() . '</a></li>';
  2482. }
  2483. }
  2484. $menu .= '</ul>';
  2485. return $menu;
  2486. }
  2487. }
  2488. // Search form
  2489. function search($text = null)
  2490. {
  2491. if(!empty($text)) {
  2492. echo <<<EOF
  2493. <form id="search-form" method="get">
  2494. <input type="text" class="search-input" name="search" value="{$text}" onfocus="if (this.value == '{$text}') {this.value = '';}" onblur="if (this.value == '') {this.value = '{$text}';}">
  2495. <input type="submit" value="{$text}" class="search-button">
  2496. </form>
  2497. EOF;
  2498. } else {
  2499. $search = i18n('Search');
  2500. echo <<<EOF
  2501. <form id="search-form" method="get">
  2502. <input type="text" class="search-input" name="search" value="{$search}" onfocus="if (this.value == '{$search}') {this.value = '';}" onblur="if (this.value == '') {this.value = '{$search}';}">
  2503. <input type="submit" value="{$search}" class="search-button">
  2504. </form>
  2505. EOF;
  2506. }
  2507. if (isset($_GET['search'])) {
  2508. $search = _h($_GET['search']);
  2509. $url = site_url() . 'search/' . remove_accent($search);
  2510. header("Location: $url");
  2511. }
  2512. }
  2513. // The not found error
  2514. function not_found($request = null)
  2515. {
  2516. if (!config('views.root')) die('HTMLy is not installed!');
  2517. $vroot = rtrim(config('views.root'), '/');
  2518. $lt = $vroot . '/layout--404.html.php';
  2519. if (file_exists($lt)) {
  2520. $layout = 'layout--404';
  2521. } else {
  2522. $layout = '';
  2523. }
  2524. header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found");
  2525. render('404', array(
  2526. 'title' => generate_title('is_default', i18n('This_page_doesnt_exist')),
  2527. 'description' => i18n('This_page_doesnt_exist'),
  2528. 'canonical' => site_url(),
  2529. 'metatags' => generate_meta(null, null),
  2530. 'breadcrumb' => '<a href="' . site_url() . '">' . config('breadcrumb.home') . '</a> &#187; ' . i18n('This_page_doesnt_exist'),
  2531. 'bodyclass' => 'error-404',
  2532. 'is_404' => true,
  2533. ), $layout);
  2534. die();
  2535. }
  2536. // Turn an array of posts into an RSS feed
  2537. function generate_rss($posts, $data = null)
  2538. {
  2539. $feed = new Feed();
  2540. $channel = new Channel();
  2541. $rssLength = config('rss.char');
  2542. $data = $data;
  2543. $rssDesc = config('rss.description');
  2544. if (is_null($data)) {
  2545. $channel
  2546. ->title(blog_title())
  2547. ->description(blog_description())
  2548. ->url(site_url())
  2549. ->appendTo($feed);
  2550. } else {
  2551. $channel
  2552. ->title($data->title)
  2553. ->description(strip_tags($data->body))
  2554. ->url($data->url)
  2555. ->appendTo($feed);
  2556. }
  2557. if ($posts) {
  2558. foreach ($posts as $p) {
  2559. $img = get_image($p->body);
  2560. if ($rssDesc == "meta") {
  2561. if (!empty($rssLength)) {
  2562. $body = shorten($p->description, $rssLength);
  2563. } else {
  2564. $body = $p->description;
  2565. }
  2566. } else {
  2567. if (!empty($rssLength)) {
  2568. $body = shorten($p->body, $rssLength);
  2569. } else {
  2570. $body = $p->body;
  2571. }
  2572. }
  2573. $item = new Item();
  2574. $item
  2575. ->category(strip_tags($p->category));
  2576. $item
  2577. ->title($p->title)
  2578. ->pubDate($p->date)
  2579. ->description($body)
  2580. ->url($p->url)
  2581. ->appendTo($channel);
  2582. if (!empty($p->image)) {
  2583. $item->enclosure($p->image, 0, "image/" . pathinfo($p->image, PATHINFO_EXTENSION));
  2584. } elseif (!empty($img)) {
  2585. $item->enclosure($img, 0, "image/" . pathinfo($img, PATHINFO_EXTENSION));
  2586. }
  2587. }
  2588. }
  2589. return $feed;
  2590. }
  2591. // Return post, archive url for sitemap
  2592. function sitemap_post_path($posts, $page = 1, $perpage = 0)
  2593. {
  2594. if (empty($posts)) {
  2595. $posts = get_blog_posts();
  2596. }
  2597. krsort($posts);
  2598. $tmp = array();
  2599. $posts = array_slice($posts, ($page - 1) * $perpage, $perpage);
  2600. foreach ($posts as $index => $v) {
  2601. $post = new stdClass;
  2602. $filepath = $v['dirname'] . '/' . $v['basename'];
  2603. // Extract the date
  2604. $arr = explode('_', $v['basename']);
  2605. // dirname string
  2606. $dirname = $v['dirname'];
  2607. // Author string
  2608. $str = explode('/', $dirname);
  2609. $author = $str[1];
  2610. $post->authorUrl = site_url() . 'author/' . $author;
  2611. $dt = str_replace($dirname, '', $arr[0]);
  2612. $t = str_replace('-', '', $dt);
  2613. $time = new DateTime($t);
  2614. $timestamp = $time->format("Y-m-d H:i:s");
  2615. // The post date
  2616. $post->date = strtotime($timestamp);
  2617. $post->lastMod = strtotime(date('Y-m-d H:i:s', filemtime($filepath)));
  2618. // The archive per month
  2619. $post->archivemonth = site_url() . 'archive/' . date('Y-m', $post->date);
  2620. // The archive per year
  2621. $post->archiveyear = site_url() . 'archive/' . date('Y', $post->date);
  2622. // The post URL
  2623. if (permalink_type() == 'default') {
  2624. $post->url = site_url() . date('Y/m', $post->date) . '/' . str_replace('.md', '', $arr[2]);
  2625. } else {
  2626. $post->url = site_url() . permalink_type() . '/' . str_replace('.md', '', $arr[2]);
  2627. }
  2628. $tmp[] = $post;
  2629. }
  2630. return $tmp;
  2631. }
  2632. // Return static page path for sitemap
  2633. function sitemap_page_path()
  2634. {
  2635. $posts = get_static_pages();
  2636. $tmp = array();
  2637. if (!empty($posts)) {
  2638. foreach ($posts as $index => $v) {
  2639. $post = new stdClass;
  2640. $fn = explode('.', $v['filename']);
  2641. if (isset($fn[1])) {
  2642. $filename = $fn[1];
  2643. } else {
  2644. $filename= $v['filename'];
  2645. }
  2646. $file = $v['dirname'] . '/' . $v['basename'];
  2647. $post->url = site_url() . $filename;
  2648. $post->lastMod = strtotime(date('Y-m-d H:i:s', filemtime($file)));
  2649. $tmp[] = $post;
  2650. $subPages = get_static_subpages($filename);
  2651. foreach ($subPages as $sIndex => $sp) {
  2652. $subpost = new stdClass;
  2653. $bs = explode('.', $sp['filename']);
  2654. if (isset($bs[1])) {
  2655. $baseSub = $bs[1];
  2656. } else {
  2657. $baseSub = $sp['filename'];
  2658. }
  2659. $urlSub = $filename . '/' . $baseSub;
  2660. $subfile = $sp['dirname'] . '/' . $sp['basename'];
  2661. $subpost->url = site_url() . $urlSub;
  2662. $subpost->lastMod = strtotime(date('Y-m-d H:i:s', filemtime($subfile)));
  2663. $tmp[] = $subpost;
  2664. }
  2665. }
  2666. }
  2667. return $tmp;
  2668. }
  2669. // Generate sitemap.xml.
  2670. function generate_sitemap($str)
  2671. {
  2672. $default_priority = '0.5';
  2673. $map = '';
  2674. header('X-Robots-Tag: noindex');
  2675. $map .= '<?xml version="1.0" encoding="UTF-8"?>';
  2676. if ($str == 'index.xml') {
  2677. $map .= '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  2678. if (config('sitemap.priority.base') !== '-1') {
  2679. $map .= '<sitemap><loc>' . site_url() . 'sitemap.base.xml</loc></sitemap>';
  2680. }
  2681. if (config('sitemap.priority.post') !== '-1') {
  2682. $map .= '<sitemap><loc>' . site_url() . 'sitemap.post.xml</loc></sitemap>';
  2683. }
  2684. if (config('sitemap.priority.static') !== '-1') {
  2685. $map .= '<sitemap><loc>' . site_url() . 'sitemap.static.xml</loc></sitemap>';
  2686. }
  2687. if (config('sitemap.priority.category') !== '-1') {
  2688. $map .= '<sitemap><loc>' . site_url() . 'sitemap.category.xml</loc></sitemap>';
  2689. }
  2690. if (config('sitemap.priority.tag') !== '-1') {
  2691. $map .= '<sitemap><loc>' . site_url() . 'sitemap.tag.xml</loc></sitemap>';
  2692. }
  2693. if (config('sitemap.priority.archiveDay') !== '-1' || config('sitemap.priority.archiveMonth') !== '-1' || config('sitemap.priority.archiveYear') !== '-1') {
  2694. $map .= '<sitemap><loc>' . site_url() . 'sitemap.archive.xml</loc></sitemap>';
  2695. }
  2696. if (config('sitemap.priority.author') !== '-1') {
  2697. $map .= '<sitemap><loc>' . site_url() . 'sitemap.author.xml</loc></sitemap>';
  2698. }
  2699. if (config('sitemap.priority.type') !== '-1') {
  2700. $map .= '<sitemap><loc>' . site_url() . 'sitemap.type.xml</loc></sitemap>';
  2701. }
  2702. $map .= '</sitemapindex>';
  2703. } elseif ($str == 'base.xml') {
  2704. $priority = (config('sitemap.priority.base')) ? config('sitemap.priority.base') : '1.0';
  2705. $map .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  2706. if ($priority !== '-1') {
  2707. $map .= '<url><loc>' . site_url() . '</loc><priority>' . $priority . '</priority></url>';
  2708. if (config('blog.enable') === 'true') {
  2709. $map .= '<url><loc>' . site_url() . blog_path() .'</loc><priority>' . $priority . '</priority></url>';
  2710. }
  2711. }
  2712. $map .= '</urlset>';
  2713. } elseif (strpos($str, 'post.') !== false ) {
  2714. if ($str == 'post.xml') {
  2715. $map .= '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  2716. $totalPosts = array();
  2717. $totalPosts = get_blog_posts();
  2718. $total = count($totalPosts);
  2719. $totalPage = ceil($total / 500);
  2720. for ($i = 1; $i <= $totalPage; $i++) {
  2721. $map .= '<sitemap><loc>' . site_url() . 'sitemap.post.'. $i .'.xml</loc></sitemap>';
  2722. }
  2723. $map .= '</sitemapindex>';
  2724. } else {
  2725. $priority = (config('sitemap.priority.post')) ? config('sitemap.priority.post') : $default_priority;
  2726. $posts = array();
  2727. $arr = explode('.', $str);
  2728. if ($priority !== '-1') {
  2729. $posts = sitemap_post_path(null, $arr[1], 500);
  2730. }
  2731. $map .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  2732. foreach ($posts as $p) {
  2733. $map .= '<url><loc>' . $p->url . '</loc><priority>' . $priority . '</priority><lastmod>' . date('Y-m-d\TH:i:sP', $p->lastMod) . '</lastmod></url>';
  2734. }
  2735. $map .= '</urlset>';
  2736. }
  2737. } elseif ($str == 'static.xml') {
  2738. $priority = (config('sitemap.priority.static')) ? config('sitemap.priority.static') : $default_priority;
  2739. $posts = array();
  2740. if ($priority !== '-1') {
  2741. $posts = sitemap_page_path();
  2742. }
  2743. $map .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  2744. foreach ($posts as $p) {
  2745. $map .= '<url><loc>' . $p->url . '</loc><priority>' . $priority . '</priority><lastmod>' . date('Y-m-d\TH:i:sP', $p->lastMod) . '</lastmod></url>';
  2746. }
  2747. $map .= '</urlset>';
  2748. } elseif ($str == 'tag.xml') {
  2749. $priority = (config('sitemap.priority.tag')) ? config('sitemap.priority.tag') : $default_priority;
  2750. $posts = array();
  2751. if ($priority !== '-1') {
  2752. $posts = get_blog_posts();
  2753. }
  2754. $tags = array();
  2755. $map .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  2756. if($posts) {
  2757. foreach ($posts as $index => $v) {
  2758. $arr = explode('_', $v['filename']);
  2759. $data = $arr[1];
  2760. $mtag = explode(',', $data);
  2761. foreach ($mtag as $etag) {
  2762. $tags[] = strtolower($etag);
  2763. }
  2764. }
  2765. foreach ($tags as $t) {
  2766. $tag[] = site_url() . 'tag/' . strtolower($t);
  2767. }
  2768. if (isset($tag)) {
  2769. $tag = array_unique($tag, SORT_REGULAR);
  2770. foreach ($tag as $t) {
  2771. $map .= '<url><loc>' . $t . '</loc><priority>' . $priority . '</priority></url>';
  2772. }
  2773. }
  2774. }
  2775. $map .= '</urlset>';
  2776. } elseif ($str == 'archive.xml') {
  2777. $priorityMonth = (config('sitemap.priority.archiveMonth')) ? config('sitemap.priority.archiveMonth') : $default_priority;
  2778. $priorityYear = (config('sitemap.priority.archiveYear')) ? config('sitemap.priority.archiveYear') : $default_priority;
  2779. $posts = sitemap_post_path(null, 1, null);
  2780. $month = array();
  2781. $year = array();
  2782. foreach ($posts as $p) {
  2783. $month[] = $p->archivemonth;
  2784. $year[] = $p->archiveyear;
  2785. }
  2786. $month = array_unique($month, SORT_REGULAR);
  2787. $year = array_unique($year, SORT_REGULAR);
  2788. $map .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  2789. if ($priorityYear !== '-1') {
  2790. foreach ($year as $y) {
  2791. $map .= '<url><loc>' . $y . '</loc><priority>' . $priorityYear . '</priority></url>';
  2792. }
  2793. }
  2794. if ($priorityMonth !== '-1') {
  2795. foreach ($month as $m) {
  2796. $map .= '<url><loc>' . $m . '</loc><priority>' . $priorityMonth . '</priority></url>';
  2797. }
  2798. }
  2799. $map .= '</urlset>';
  2800. } elseif ($str == 'author.xml') {
  2801. $priority = (config('sitemap.priority.author')) ? config('sitemap.priority.author') : $default_priority;
  2802. $author = array();
  2803. if ($priority !== '-1') {
  2804. $posts = sitemap_post_path(null, 1, null);
  2805. foreach ($posts as $p) {
  2806. $author[] = $p->authorUrl;
  2807. }
  2808. $author = array_unique($author, SORT_REGULAR);
  2809. }
  2810. $map .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  2811. if ($priority !== '-1') {
  2812. foreach ($author as $a) {
  2813. $map .= '<url><loc>' . $a . '</loc><priority>' . $priority . '</priority></url>';
  2814. }
  2815. }
  2816. $map .= '</urlset>';
  2817. } elseif ($str == 'category.xml') {
  2818. $priority = (config('sitemap.priority.category')) ? config('sitemap.priority.category') : $default_priority;
  2819. $posts = array();
  2820. if ($priority !== '-1') {
  2821. $posts = get_blog_posts();
  2822. }
  2823. $cats = array();
  2824. $map .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  2825. if($posts) {
  2826. foreach ($posts as $index => $v) {
  2827. $dirname = $v['dirname'];
  2828. $str = explode('/', $dirname);
  2829. $cats[] = $str[3];
  2830. }
  2831. foreach ($cats as $c) {
  2832. $cat[] = site_url() . 'category/' . strtolower($c);
  2833. }
  2834. if (isset($cat)) {
  2835. $cat = array_unique($cat, SORT_REGULAR);
  2836. foreach ($cat as $c) {
  2837. $map .= '<url><loc>' . $c . '</loc><priority>' . $priority . '</priority></url>';
  2838. }
  2839. }
  2840. }
  2841. $map .= '</urlset>';
  2842. } elseif ($str == 'type.xml') {
  2843. $priority = (config('sitemap.priority.type')) ? config('sitemap.priority.type') : $default_priority;
  2844. $posts = array();
  2845. if ($priority !== '-1') {
  2846. $posts = get_blog_posts();
  2847. }
  2848. $cats = array();
  2849. $map .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  2850. if($posts) {
  2851. foreach ($posts as $index => $v) {
  2852. $dirname = $v['dirname'];
  2853. $str = explode('/', $dirname);
  2854. $types[] = $str[4];
  2855. }
  2856. foreach ($types as $t) {
  2857. $type[] = site_url() . 'type/' . strtolower($t);
  2858. }
  2859. if (isset($type)) {
  2860. $type = array_unique($type, SORT_REGULAR);
  2861. foreach ($type as $t) {
  2862. $map .= '<url><loc>' . $t . '</loc><priority>' . $priority . '</priority></url>';
  2863. }
  2864. }
  2865. }
  2866. $map .= '</urlset>';
  2867. }
  2868. echo $map;
  2869. }
  2870. // Function to generate OPML file
  2871. function generate_opml()
  2872. {
  2873. $opml_data = array(
  2874. 'head' => array(
  2875. 'title' => blog_title() . ' OPML File',
  2876. 'ownerName' => blog_title(),
  2877. 'ownerId' => site_url()
  2878. ),
  2879. 'body' => array(
  2880. array(
  2881. 'text' => blog_title(),
  2882. 'description' => blog_description(),
  2883. 'htmlUrl' => site_url(),
  2884. 'language' => 'unknown',
  2885. 'title' => blog_title(),
  2886. 'type' => 'rss',
  2887. 'version' => 'RSS2',
  2888. 'xmlUrl' => site_url() . 'feed/rss'
  2889. )
  2890. )
  2891. );
  2892. $opml = new opml($opml_data);
  2893. return $opml->render();
  2894. }
  2895. // Turn an array of posts into a JSON
  2896. function generate_json($posts)
  2897. {
  2898. return json_encode($posts);
  2899. }
  2900. // TRUE if the current page is an index page like frontpage, tag index, archive index and search index.
  2901. function is_index()
  2902. {
  2903. if (blog_path() == permalink_type()) {
  2904. $req = rtrim(strtok($_SERVER["REQUEST_URI"], '?'), '/') . '/';
  2905. $in = explode('/' . blog_path(), $req);
  2906. if (isset($in[1])) {
  2907. if ($in[1] == '/') {
  2908. return true;
  2909. }
  2910. }
  2911. if (stripos($req, '/category/') !== false || stripos($req, '/archive/') !== false || stripos($req, '/tag/') !== false || stripos($req, '/search/') !== false || stripos($req, '/type/') !== false || $req == site_path() . '/') {
  2912. return true;
  2913. } else {
  2914. return false;
  2915. }
  2916. } else {
  2917. $req = strtok($_SERVER["REQUEST_URI"], '?');
  2918. if (stripos($req, '/category/') !== false || stripos($req, '/archive/') !== false || stripos($req, '/tag/') !== false || stripos($req, '/search/') !== false || stripos($req, '/type/') !== false || stripos($req, '/' . blog_path()) !== false || $req == site_path() . '/') {
  2919. return true;
  2920. } else {
  2921. return false;
  2922. }
  2923. }
  2924. }
  2925. // Return post permalink type
  2926. function permalink_type()
  2927. {
  2928. $permalink = config('permalink.type');
  2929. if (!is_null($permalink) && !empty($permalink)) {
  2930. return strtolower(str_replace('/', '', $permalink));
  2931. }
  2932. return 'default';
  2933. }
  2934. // Return blog path index
  2935. function blog_path()
  2936. {
  2937. $path = config('blog.path');
  2938. if (!is_null($path) && !empty($path)) {
  2939. return strtolower(str_replace('/', '', $path));
  2940. }
  2941. return 'blog';
  2942. }
  2943. // Return blog string
  2944. function blog_string()
  2945. {
  2946. $string = config('blog.string');
  2947. if (!is_null($string) && !empty($string)) {
  2948. return safe_html($string);
  2949. }
  2950. return 'Blog';
  2951. }
  2952. // Return blog title
  2953. function blog_title()
  2954. {
  2955. return safe_html(config('blog.title'));
  2956. }
  2957. // Return blog tagline
  2958. function blog_tagline()
  2959. {
  2960. return safe_html(config('blog.tagline'));
  2961. }
  2962. // Return blog description
  2963. function blog_description()
  2964. {
  2965. return config('blog.description');
  2966. }
  2967. // Return blog copyright
  2968. function blog_copyright()
  2969. {
  2970. return config('blog.copyright');
  2971. }
  2972. // Return blog language
  2973. function blog_language()
  2974. {
  2975. $lang = config('language');
  2976. if (!empty($lang)) {
  2977. $exp = explode('_', $lang);
  2978. return $exp[0] . '-' . $exp[1];
  2979. }
  2980. return 'en-US';
  2981. }
  2982. // Return locale language
  2983. function locale_language()
  2984. {
  2985. $lang = config('language');
  2986. if (!empty($lang)) {
  2987. $exp = explode('_', $lang);
  2988. return $exp[0] . '_' . $exp[1];
  2989. }
  2990. return 'en_US';
  2991. }
  2992. // Output head contents
  2993. function head_contents()
  2994. {
  2995. $output = '';
  2996. $google_wmt_id = config('google.wmt.id');
  2997. $bing_wmt_id = config('bing.wmt.id');
  2998. if (config('show.version') !== 'false') {
  2999. $version = 'HTMLy ' . constant('HTMLY_VERSION');
  3000. } else {
  3001. $version = 'HTMLy';
  3002. }
  3003. $favicon = config('favicon.image');
  3004. if (empty($favicon)) {
  3005. $favicon = '<link rel="icon" type="image/png" href="' . site_url() . 'favicon.png" />' . "\n";
  3006. } else {
  3007. $favicon = '<link rel="icon" type="image/'. pathinfo($favicon, PATHINFO_EXTENSION) .'" href="' . $favicon . '" />' . "\n";
  3008. }
  3009. $output .= '<meta charset="utf-8" />' . "\n";
  3010. $output .= '<meta http-equiv="X-UA-Compatible" content="IE=edge" />' . "\n";
  3011. $output .= '<meta name="viewport" content="width=device-width, initial-scale=1" />' . "\n";
  3012. $output .= '<meta name="generator" content="' . $version . '" />' . "\n";
  3013. $output .= $favicon;
  3014. $output .= '<link rel="sitemap" href="' . site_url() . 'sitemap.xml" />' . "\n";
  3015. $output .= '<link rel="alternate" type="application/rss+xml" title="' . safe_html(blog_title()) . ' Feed" href="' . site_url() . 'feed/rss" />' . "\n";
  3016. if (!empty($google_wmt_id)) {
  3017. $output .= '<meta name="google-site-verification" content="' . $google_wmt_id . '" />' . "\n";
  3018. }
  3019. if (!empty($bing_wmt_id)) {
  3020. $output .= '<meta name="msvalidate.01" content="' . $bing_wmt_id . '" />' . "\n";
  3021. }
  3022. return $output;
  3023. }
  3024. // File cache
  3025. function file_cache($request)
  3026. {
  3027. if (config('cache.off') == 'true') return;
  3028. $hour = config('cache.expiration');
  3029. if (empty($hour)) {
  3030. $hour = 6;
  3031. }
  3032. $now = time();
  3033. $c = str_replace('/', '#', str_replace('?', '~', rawurldecode($request)));
  3034. $cachefile = 'cache/page/' . $c . '.cache';
  3035. if (file_exists($cachefile)) {
  3036. if ($now - filemtime($cachefile) >= 60 * 60 * $hour) {
  3037. unlink($cachefile);
  3038. } else {
  3039. header('Content-type: text/html; charset=utf-8');
  3040. readfile($cachefile);
  3041. die;
  3042. }
  3043. }
  3044. }
  3045. // Add page views count
  3046. function add_view($page)
  3047. {
  3048. $dir = 'content/data/';
  3049. if (!is_dir($dir)) {
  3050. mkdir($dir, 0775, true);
  3051. }
  3052. $filename = "content/data/views.json";
  3053. $views = array();
  3054. if (file_exists($filename)) {
  3055. $views = json_decode(file_get_data($filename), true);
  3056. }
  3057. if (isset($views['flock_fail'])) {
  3058. return;
  3059. } else {
  3060. if (isset($views[$page])) {
  3061. $views[$page]++;
  3062. save_json_pretty($filename, $views);
  3063. } else {
  3064. $views[$page] = 1;
  3065. save_json_pretty($filename, $views);
  3066. }
  3067. }
  3068. }
  3069. // Get the page views count
  3070. function get_views($page, $views = null)
  3071. {
  3072. $filename = "content/data/views.json";
  3073. if (is_null($views)) {
  3074. if (file_exists($filename)) {
  3075. $views = json_decode(file_get_contents($filename), true);
  3076. }
  3077. if (isset($views[$page])) {
  3078. return $views[$page];
  3079. }
  3080. } else {
  3081. if (isset($views[$page])) {
  3082. return $views[$page];
  3083. }
  3084. }
  3085. return -1;
  3086. }
  3087. // Get tag inside the markdown files
  3088. function get_content_tag($tag, $string, $alt = null)
  3089. {
  3090. $reg = '/\<!--' . $tag . '(.+)' . $tag . '--\>/';
  3091. $ary = array();
  3092. if (preg_match($reg, $string, $ary)) {
  3093. if (isset($ary[1])) {
  3094. $result = trim($ary[1]);
  3095. if (!empty($result)) {
  3096. return $result;
  3097. }
  3098. }
  3099. }
  3100. return $alt;
  3101. }
  3102. // Strip html comment
  3103. function remove_html_comments($content)
  3104. {
  3105. $patterns = array('/(\s|)<!--(?!(toc|more)-->).*-->(\s|)/');
  3106. return preg_replace($patterns, '', $content);
  3107. }
  3108. function get_field($key, $object = null)
  3109. {
  3110. if (is_null($object)) {
  3111. return false;
  3112. }
  3113. return (get_content_tag($key, $object));
  3114. }
  3115. // Google recaptcha
  3116. function isCaptcha($reCaptchaResponse)
  3117. {
  3118. $url = "https://www.google.com/recaptcha/api/siteverify";
  3119. $options = array(
  3120. "secret" => config("login.protect.private"),
  3121. "response" => $reCaptchaResponse,
  3122. "remoteip" => $_SERVER['REMOTE_ADDR'],
  3123. );
  3124. $fileContent = @file_get_contents($url . "?" . http_build_query($options));
  3125. if ($fileContent === false) {
  3126. return false;
  3127. }
  3128. $json = json_decode($fileContent, true);
  3129. if ($json == false) {
  3130. return false;
  3131. }
  3132. return ($json['success']);
  3133. }
  3134. // Cloudflare Turnstile
  3135. function isTurnstile($turnstileResponse)
  3136. {
  3137. $public = config("login.protect.public");
  3138. $private = config("login.protect.private");
  3139. $ip = $_SERVER['REMOTE_ADDR'];
  3140. $url = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';
  3141. $data = array('secret' => $private, 'response' => $turnstileResponse, 'remoteip' => $ip);
  3142. $query = http_build_query($data);
  3143. $options = array(
  3144. 'http' => array(
  3145. 'header' => "Content-Type: application/x-www-form-urlencoded\r\n".
  3146. "Content-Length: ".strlen($query)."\r\n".
  3147. "User-Agent:HTMLy/1.0\r\n",
  3148. 'method' => "POST",
  3149. 'content' => $query,
  3150. )
  3151. );
  3152. $stream = stream_context_create($options);
  3153. $fileContent = file_get_contents($url, false, $stream);
  3154. if ($fileContent === false) {
  3155. return false;
  3156. }
  3157. $json = json_decode($fileContent, true);
  3158. if ($json == false) {
  3159. return false;
  3160. }
  3161. return ($json['success']);
  3162. }
  3163. // Get video ID
  3164. function get_video_id($url)
  3165. {
  3166. if(empty($url)) {
  3167. return;
  3168. }
  3169. $link = parse_url($url);
  3170. if(!isset($link['host'])) {
  3171. return $url;
  3172. }
  3173. if (stripos($link['host'], 'youtube.com') !== false || stripos($link['host'], 'youtu.be') !== false) {
  3174. preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match);
  3175. return $match[1];
  3176. } elseif (stripos($link['host'], 'vimeo.com') !== false) {
  3177. preg_match('%^https?:\/\/(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|video\/|)(\d+)(?:$|\/|\?)(?:[?]?.*)$%im', $url, $match);
  3178. return $match[3];
  3179. } else {
  3180. return $url;
  3181. }
  3182. }
  3183. // translate tag to i18n
  3184. function tag_i18n($tag)
  3185. {
  3186. static $tags = array();
  3187. $filename = "content/data/tags.lang";
  3188. if (file_exists($filename)) {
  3189. $tags = unserialize(file_get_contents($filename));
  3190. }
  3191. if (isset($tags[$tag])) {
  3192. return $tags[$tag];
  3193. }
  3194. return $tag;
  3195. }
  3196. // return html safe string
  3197. function safe_html($string)
  3198. {
  3199. $string = htmlspecialchars($string, ENT_QUOTES);
  3200. $string = preg_replace('/\r\n|\r|\n/', ' ', $string);
  3201. $string = preg_replace('/\s\s+/', ' ', $string);
  3202. $string = ltrim(rtrim($string));
  3203. return $string;
  3204. }
  3205. // Replace href
  3206. function replace_href($string, $tag, $class, $url)
  3207. {
  3208. libxml_use_internal_errors(true);
  3209. // Load the HTML in DOM
  3210. $doc = new DOMDocument();
  3211. $doc->loadHTML('<span class="dom-charset"><meta charset="utf8"></span>' . $string);
  3212. $output = '';
  3213. // Then select all anchor tags
  3214. $all_anchor_tags = $doc->getElementsByTagName($tag);
  3215. foreach ($all_anchor_tags as $_tag) {
  3216. if ($_tag->getAttribute('class') == $class) {
  3217. // If match class get the href value
  3218. $old = $_tag->getAttribute('href');
  3219. $new = $_tag->setAttribute('href', $url . mb_convert_encoding($old, 'UTF-8'));
  3220. }
  3221. }
  3222. $output = preg_replace('~<(?:!DOCTYPE|/?(?:html|head|body))[^>]*>\s*~i', '', mb_convert_encoding($doc->saveHTML($doc->documentElement), 'UTF-8'));
  3223. return str_replace('<span class="dom-charset"><meta charset="utf8"></span>', '', $output);
  3224. }
  3225. // Format the date
  3226. function format_date($date, $dateFormat = null)
  3227. {
  3228. if (empty($dateFormat)) {
  3229. $dateFormat = config('date.format');
  3230. }
  3231. if (extension_loaded('intl')) {
  3232. $format_map = array('s' => 'ss', 'i' => 'mm', 'H' => 'HH', 'G' => 'H', 'd' => 'dd', 'j' => 'd', 'D' => 'EE', 'l' => 'EEEE', 'm' => 'MM', 'M' => 'MMM', 'F' => 'MMMM', 'Y' => 'yyyy');
  3233. $intlFormat = strtr($dateFormat, $format_map);
  3234. $formatter = new IntlDateFormatter(locale_language(), IntlDateFormatter::NONE, IntlDateFormatter::NONE, config('timezone'), IntlDateFormatter::GREGORIAN, $intlFormat);
  3235. return $formatter->format($date);
  3236. } else {
  3237. return date($dateFormat, $date);
  3238. }
  3239. }
  3240. // Publish scheduled post
  3241. function publish_scheduled()
  3242. {
  3243. $posts = get_scheduled_posts();
  3244. if (!empty($posts)) {
  3245. foreach ($posts as $index => $v) {
  3246. $str = explode('_', $v['basename']);
  3247. $old = $v['dirname'] . '/' . $v['basename'];
  3248. $new = dirname($v['dirname']) . '/' . $v['basename'];
  3249. $t = str_replace('-', '', $str[0]);
  3250. $time = new DateTime($t);
  3251. $timestamp = $time->format("Y m d H:i:s");
  3252. if (date('Y m d H:i:s') >= $timestamp) {
  3253. rename($old, $new);
  3254. rebuilt_cache('all');
  3255. clear_cache();
  3256. }
  3257. }
  3258. }
  3259. }
  3260. // Insert toc
  3261. function insert_toc($id, $part_1 = null, $part_2 = null)
  3262. {
  3263. $state = config('toc.state');
  3264. if ($state !== 'open') {
  3265. $state = '';
  3266. }
  3267. $label = config('toc.label');
  3268. if (empty($label)) {
  3269. $label = 'Table of Contents';
  3270. }
  3271. $style = config('toc.style');
  3272. if ($style == 'default' || empty($style)) {
  3273. $style = '<link rel="stylesheet" id="default-toc-style" href="'. site_url() .'system/resources/css/toc.css" type="text/css" media="all">';
  3274. } else {
  3275. $style = '';
  3276. }
  3277. $load = <<<EOF
  3278. <script>
  3279. document.addEventListener("DOMContentLoaded", function() {
  3280. if (document.getElementById('toc-wrapper.{$id}').parentNode.classList.contains('{$id}')) {
  3281. generateTOC('.{$id}');
  3282. } else {
  3283. document.getElementById('toc-wrapper.{$id}').parentNode.classList.add('{$id}');
  3284. generateTOC('.{$id}');
  3285. }
  3286. });
  3287. </script>
  3288. EOF;
  3289. $result = $part_1 . '<div class="toc-wrapper" id="toc-wrapper.'. $id .'" style="display:none;" >'. $load . $style .'<details '. $state .'><summary title="'. $label .'"><span class="details">'. $label .'</span></summary><div class="inner"><div class="toc" id="toc.'. $id .'"></div></div></details><script src="'. site_url().'system/resources/js/toc.generator.js"></script></div>' . $part_2;
  3290. return $result;
  3291. }
  3292. // Automatically add toc after x paragraph
  3293. function automatic_toc($content, $id)
  3294. {
  3295. $pos = config('toc.position');
  3296. $exp = explode('</p>', $content);
  3297. if (is_null($pos) || $pos > count($exp)){
  3298. return $content;
  3299. }
  3300. array_splice($exp, $pos, 0, insert_toc($id) . '<p>');
  3301. $content = implode('</p>', $exp);
  3302. return $content;
  3303. }
  3304. function generate_title($type = null, $object = null)
  3305. {
  3306. if ($type == 'is_front') {
  3307. $format = config('home.title.format');
  3308. if (empty($format)) {
  3309. $format = '%blog_title% - %blog_tagline%';
  3310. }
  3311. $var_map = array('%blog_title%' => blog_title(), '%blog_tagline%' => blog_tagline(), '%blog_description%' => blog_description());
  3312. $var = strtr($format, $var_map);
  3313. return strip_tags($var);
  3314. } elseif ($type == 'is_post') {
  3315. $format = config('post.title.format');
  3316. if (empty($format)) {
  3317. $format = '%post_title% - %blog_title%';
  3318. }
  3319. $var_map = array('%blog_title%' => blog_title(), '%blog_tagline%' => blog_tagline(), '%blog_description%' => blog_description(), '%post_title%' => $object->title, '%post_description%' => $object->description, '%post_category%' => $object->categoryTitle, '%post_tag%' => $object->tag, '%post_author%' => $object->authorName, '%post_type%' => ucfirst($object->type));
  3320. $var = strtr($format, $var_map);
  3321. return strip_tags($var);
  3322. } elseif ($type == 'is_page' || $type == 'is_subpage') {
  3323. $format = config('page.title.format');
  3324. if (empty($format)) {
  3325. $format = '%page_title% - %blog_title%';
  3326. }
  3327. $var_map = array('%blog_title%' => blog_title(), '%blog_tagline%' => blog_tagline(), '%blog_description%' => blog_description(), '%page_title%' => $object->title, '%page_description%' => $object->description);
  3328. $var = strtr($format, $var_map);
  3329. return strip_tags($var);
  3330. } elseif ($type == 'is_profile') {
  3331. $format = config('profile.title.format');
  3332. if (empty($format)) {
  3333. $format = '%author_name% - %blog_title%';
  3334. }
  3335. $var_map = array('%blog_title%' => blog_title(), '%blog_tagline%' => blog_tagline(), '%blog_description%' => blog_description(), '%author_name%' => $object->title, '%author_description%' => $object->description);
  3336. $var = strtr($format, $var_map);
  3337. return strip_tags($var);
  3338. } elseif ($type == 'is_category') {
  3339. $format = config('category.title.format');
  3340. if (empty($format)) {
  3341. $format = '%category_title% - %blog_title%';
  3342. }
  3343. $var_map = array('%blog_title%' => blog_title(), '%blog_tagline%' => blog_tagline(), '%blog_description%' => blog_description(), '%category_title%' => $object->title, '%category_description%' => $object->description);
  3344. $var = strtr($format, $var_map);
  3345. return strip_tags($var);
  3346. } elseif ($type == 'is_tag') {
  3347. $format = config('tag.title.format');
  3348. if (empty($format)) {
  3349. $format = '%tag_title% - %blog_title%';
  3350. }
  3351. $var_map = array('%blog_title%' => blog_title(), '%blog_tagline%' => blog_tagline(), '%blog_description%' => blog_description(), '%tag_title%' => $object->title, '%tag_description%' => $object->description);
  3352. $var = strtr($format, $var_map);
  3353. return strip_tags($var);
  3354. } elseif ($type == 'is_archive') {
  3355. $format = config('archive.title.format');
  3356. if (empty($format)) {
  3357. $format = '%archive_title% - %blog_title%';
  3358. }
  3359. $var_map = array('%blog_title%' => blog_title(), '%blog_tagline%' => blog_tagline(), '%blog_description%' => blog_description(), '%archive_title%' => $object->title, '%archive_description%' => $object->description);
  3360. $var = strtr($format, $var_map);
  3361. return strip_tags($var);
  3362. } elseif ($type == 'is_search') {
  3363. $format = config('search.title.format');
  3364. if (empty($format)) {
  3365. $format = '%search_title% - %blog_title%';
  3366. }
  3367. $var_map = array('%blog_title%' => blog_title(), '%blog_tagline%' => blog_tagline(), '%blog_description%' => blog_description(), '%search_title%' => $object->title, '%search_description%' => $object->description);
  3368. $var = strtr($format, $var_map);
  3369. return strip_tags($var);
  3370. } elseif ($type == 'is_type') {
  3371. $format = config('type.title.format');
  3372. if (empty($format)) {
  3373. $format = '%type_title% - %blog_title%';
  3374. }
  3375. $var_map = array('%blog_title%' => blog_title(), '%blog_tagline%' => blog_tagline(), '%blog_description%' => blog_description(), '%type_title%' => $object->title, '%type_description%' => $object->description);
  3376. $var = strtr($format, $var_map);
  3377. return strip_tags($var);
  3378. } elseif ($type == 'is_blog') {
  3379. $format = config('blog.title.format');
  3380. if (empty($format)) {
  3381. $format = 'Blog - %blog_title%';
  3382. }
  3383. $var_map = array('%blog_title%' => blog_title(), '%blog_tagline%' => blog_tagline(), '%blog_description%' => blog_description());
  3384. $var = strtr($format, $var_map);
  3385. return strip_tags($var);
  3386. } elseif ($type == 'is_default') {
  3387. $format = config('default.title.format');
  3388. if (empty($format)) {
  3389. $format = '%page_title% - %blog_title%';
  3390. }
  3391. $var_map = array('%blog_title%' => blog_title(), '%blog_tagline%' => blog_tagline(), '%blog_description%' => blog_description(), '%page_title%' => $object);
  3392. $var = strtr($format, $var_map);
  3393. return strip_tags($var);
  3394. }
  3395. }
  3396. function generate_meta($type = null, $object = null)
  3397. {
  3398. $tags = '';
  3399. $defaultImg = config('default.image');
  3400. if (empty($defaultImg)) {
  3401. $defaultImg = site_url() . 'system/resources/images/logo-big.png';
  3402. }
  3403. $fbApp = config('fb.appid');
  3404. $facebook = config('social.facebook');
  3405. $twitter = config('social.twitter');
  3406. if (is_null($object)) {
  3407. if ($type == 'is_blog') {
  3408. $tags .= '<title>'. generate_title('is_blog', null) .'</title>' . "\n";
  3409. $tags .= '<link rel="canonical" href="'. site_url() . blog_path() .'" />' . "\n";
  3410. $tags .= '<meta name="description" content="'. blog_title() . ' ' . blog_string() .'"/>' . "\n";
  3411. $tags .= '<meta property="og:title" content="'. safe_html(generate_title('is_blog', null)) . '" />' . "\n";
  3412. $tags .= '<meta property="og:description" content="'. blog_title() . ' ' . blog_string() .'" />' . "\n";
  3413. $tags .= '<meta property="og:url" content="'. site_url() . blog_path() .'" />' . "\n";
  3414. } else {
  3415. $tags .= '<title>'. generate_title('is_front', null) .'</title>' . "\n";
  3416. $tags .= '<link rel="canonical" href="'. site_url() .'" />' . "\n";
  3417. $tags .= '<meta name="description" content="'. safe_html(strip_tags(blog_description())) .'"/>' . "\n";
  3418. $tags .= '<meta property="og:title" content="'. safe_html(generate_title('is_front', null)) . '" />' . "\n";
  3419. $tags .= '<meta property="og:description" content="'. safe_html(strip_tags(blog_description())) .'" />' . "\n";
  3420. $tags .= '<meta property="og:url" content="'. site_url() .'" />' . "\n";
  3421. }
  3422. $tags .= '<meta property="og:locale" content="'. locale_language() .'" />' . "\n";
  3423. $tags .= '<meta property="og:type" content="website" />' . "\n";
  3424. $tags .= '<meta property="og:site_name" content="'. blog_title() . '" />' . "\n";
  3425. $tags .= '<meta property="og:image" content="'. $defaultImg .'" />' . "\n";
  3426. $tags .= '<meta name="twitter:card" content="summary_large_image" />' . "\n";
  3427. if(!empty($twitter)) {
  3428. $twitter = parse_url($twitter);
  3429. $tags .= '<meta name="twitter:site" content="'. str_replace('/', '@', $twitter['path']) .'" />' . "\n";
  3430. }
  3431. if (!empty($fbApp)) {
  3432. $tags .= '<meta property="fb:app_id" content="'. $fbApp .'" />' . "\n";
  3433. }
  3434. } else {
  3435. if(!empty($object->image)) {
  3436. $image = $object->image;
  3437. } else {
  3438. $image = get_image($object->body);
  3439. if(empty($image)) {
  3440. $image = $defaultImg;
  3441. }
  3442. }
  3443. if ($type == 'is_post') {
  3444. $tags .= '<title>'. generate_title('is_post',$object) .'</title>' . "\n";
  3445. $tags .= '<meta name="author" content="'. safe_html($object->authorName) .'" />' . "\n";
  3446. $tags .= '<meta name="article:published_time" content="'. date('c', $object->date) .'" />' . "\n";
  3447. $tags .= '<meta name="article:modified_time" content="'. date('c', $object->lastMod) .'" />' . "\n";
  3448. $tags .= '<meta name="article:section" content="'. safe_html($object->categoryTitle) .'" />' . "\n";
  3449. $tags .= '<meta name="article:section_url" content="'. $object->categoryUrl .'" />' . "\n";
  3450. } elseif ($type == 'is_page' || $type == 'is_subpage') {
  3451. $tags .= '<title>'. generate_title('is_page',$object) .'</title>' . "\n";
  3452. $tags .= '<meta name="article:modified_time" content="'. date('c', $object->lastMod) .'" />' . "\n";
  3453. } else {
  3454. $tags .= '<title>'. generate_title($type , $object) .'</title>' . "\n";
  3455. }
  3456. $tags .= '<link rel="canonical" href="'. $object->url .'" />' . "\n";
  3457. $tags .= '<meta name="description" content="'. safe_html($object->description) .'"/>' . "\n";
  3458. if(!empty($facebook)) {
  3459. $tags .= '<meta property="article:publisher" content="'. $facebook .'" />' . "\n";
  3460. }
  3461. if(!empty($twitter)) {
  3462. $twitter = parse_url($twitter);
  3463. $tags .= '<meta name="twitter:creator" content="'. str_replace('/', '@', $twitter['path']) .'" />' . "\n";
  3464. $tags .= '<meta name="twitter:site" content="'. str_replace('/', '@', $twitter['path']) .'" />' . "\n";
  3465. }
  3466. $tags .= '<meta property="og:locale" content="'. locale_language() .'" />' . "\n";
  3467. $tags .= '<meta property="og:site_name" content="'. blog_title() . '" />' . "\n";
  3468. $tags .= '<meta property="og:type" content="article" />' . "\n";
  3469. $tags .= '<meta property="og:title" content="'. safe_html($object->title) .'" />' . "\n";
  3470. $tags .= '<meta property="og:url" content="'. $object->url .'" />' . "\n";
  3471. $tags .= '<meta property="og:description" content="'. safe_html($object->description) .'" />' . "\n";
  3472. $tags .= '<meta name="twitter:card" content="summary_large_image" />' . "\n";
  3473. $tags .= '<meta property="og:image" content="'. $image .'" />' . "\n";
  3474. if (!empty($fbApp)) {
  3475. $tags .= '<meta property="fb:app_id" content="'. $fbApp .'" />' . "\n";
  3476. }
  3477. }
  3478. return $tags;
  3479. }