A tumblelog CMS built on AJAX, PHP and MySQL.

functions.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. <?php
  2. if(!defined('entry') || !entry) die('Not a valid page');
  3. /* ===========================
  4. gelato CMS - A PHP based tumblelog CMS
  5. development version
  6. http://www.gelatocms.com/
  7. gelato CMS is a free software licensed under the GPL 2.0
  8. Copyright (C) 2007 by Pedro Santana <pecesama at gmail dot com>
  9. =========================== */
  10. ?>
  11. <?php
  12. function version() {
  13. return "1.0";
  14. }
  15. function codeName() {
  16. return "vaniglia RC1";
  17. }
  18. function beginsWith($str, $sub) {
  19. return (strpos($str, $sub) === 0);
  20. }
  21. function endsWith($str, $sub) {
  22. return (substr($str, strlen($str) - strlen($sub)) == $sub);
  23. }
  24. function getFileName($fileUrl) {
  25. $path = explode('/', $fileUrl);
  26. return $path[count($path)-1];
  27. }
  28. function isMP3($fileUrl) {
  29. if (endsWith($fileUrl, ".mp3")) {
  30. return true;
  31. } else {
  32. return false;
  33. }
  34. }
  35. function getMP3File($remoteFileName) {
  36. if (isMP3($remoteFileName)) {
  37. if (getFile($remoteFileName)) {
  38. return true;
  39. } else {
  40. return false;
  41. }
  42. } elseif (isGoEar($remoteFileName)) {
  43. return true;
  44. } elseif (isOdeo($remoteFileName)) {
  45. return true;
  46. } else {
  47. return false;
  48. }
  49. }
  50. function getGoEarCode($songUrl) {
  51. $pos = strpos($songUrl, "?v=");
  52. $lon = strlen($songUrl);
  53. $str = substr($songUrl, $pos + 3, $lon);
  54. return $str;
  55. }
  56. function isGoEar($songUrl) {
  57. if (beginsWith($songUrl, "http://www.goear.com/listen.php?v=") || beginsWith($songUrl, "http://goear.com/listen.php?v="))
  58. return true;
  59. else
  60. return false;
  61. }
  62. function isOdeo($songUrl){
  63. if (beginsWith($songUrl, "http://odeo.com/audio/") || beginsWith($songUrl, "http://www.odeo.com/audio/"))
  64. return true;
  65. else
  66. return false;
  67. }
  68. function getOdeoCode($songUrl) {
  69. $params = explode("audio/", $songUrl);
  70. $params2 = explode("/",$params[1]);
  71. return $params2[0];
  72. }
  73. function isImageFile($photoUrl) {
  74. if (endsWith($photoUrl, ".jpg")) { return true; }
  75. elseif (endsWith($photoUrl, ".gif")) { return true; }
  76. elseif (endsWith($photoUrl, ".png")) { return true; }
  77. else { return false; }
  78. }
  79. function getPhotoFile($remoteFileName) {
  80. if (isImageFile($remoteFileName)) {
  81. if (getFile($remoteFileName)) {
  82. return true;
  83. } else {
  84. return false;
  85. }
  86. } else {
  87. return false;
  88. }
  89. }
  90. function getFile($remoteFileName) {
  91. $fileName = "../uploads/".sanitizeName(getFileName($remoteFileName));
  92. $str = _file_get_contents($remoteFileName);
  93. if (!$handle = fopen($fileName, 'w')) {
  94. //die("no se abrio de escritura");
  95. return false;
  96. }
  97. if (fwrite($handle, $str) === FALSE) {
  98. //die("no se escribio");
  99. return false;
  100. }
  101. fclose($handle);
  102. return true;
  103. }
  104. function isVimeoVideo($videoUrl) {
  105. if (beginsWith($videoUrl, "http://vimeo.com/clip:") || beginsWith($videoUrl, "http://www.vimeo.com/clip:"))
  106. return true;
  107. else
  108. return false;
  109. }
  110. function getVimeoVideoUrl($videoUrl) {
  111. return array_pop(explode("clip:",$videoUrl));
  112. }
  113. function isYoutubeVideo($videoUrl) {
  114. if (beginsWith($videoUrl, "http://youtube.com/watch?v=") || beginsWith($videoUrl, "http://www.youtube.com/watch?v="))
  115. return true;
  116. else
  117. return false;
  118. }
  119. function isYahooVideo($videoUrl){
  120. if (beginsWith($videoUrl, "http://video.yahoo.com/watch/") || beginsWith($videoUrl, "http://www.video.yahoo.com/watch/"))
  121. return true;
  122. else
  123. return false;
  124. }
  125. function getYahooVideoCode($videoUrl){
  126. $params = explode("http://video.yahoo.com/watch/", $videoUrl);
  127. $params2 = explode("/",$params[1]);
  128. $values[0] = $params2[0];
  129. $values[1] = $params2[1];
  130. return $values;
  131. }
  132. function getYoutubeVideoUrl($videoUrl) {
  133. $params = explode("?v=", $videoUrl);
  134. $params2 = explode("&",$params[1]);
  135. return $params2[0];
  136. }
  137. function isDailymotionVideo($videoUrl) {
  138. if (beginsWith($videoUrl, "http://www.dailymotion.com/video/") || beginsWith($videoUrl, "http://dailymotion.com/video/"))
  139. return true;
  140. else
  141. return false;
  142. }
  143. function getDailymotionVideoUrl($videoUrl) {
  144. $params = explode("video/", $videoUrl);
  145. $params2 = explode("_",$params[1]);
  146. return $params2[0];
  147. }
  148. function isSlideSharePresentation($videoUrl) {
  149. if (beginsWith($videoUrl, "[slideshare id="))
  150. return true;
  151. else
  152. return false;
  153. }
  154. function getSlideSharePresentationCode($videoUrl) {
  155. $videoUrl = str_replace("[slideshare id=", "", $videoUrl);
  156. $videoUrl = str_replace("&doc=", " ", $videoUrl);
  157. $videoUrl = str_replace("&w=", " ", $videoUrl);
  158. return explode(" ",$videoUrl);
  159. }
  160. function isVideo($url) {
  161. if (isYoutubeVideo($url)) { return true; }
  162. elseif (isVimeoVideo($url)) { return true; }
  163. elseif (isDailymotionVideo($url)) { return true; }
  164. elseif (isYahooVideo($url)) { return true; }
  165. elseif (isSlideSharePresentation($url)) { return true; }
  166. else { return false; }
  167. }
  168. function sendMail($to, $title, $body, $from) {
  169. $rp = trim($from);
  170. $org = "gelato CMS";
  171. $mailer = "gelato CMS Mailer";
  172. $head = '';
  173. $head .= "Content-Type: text/html \r\n";
  174. $head .= "Date: ". date('r'). " \r\n";
  175. $head .= "Return-Path: $rp \r\n";
  176. $head .= "From: $from \r\n";
  177. $head .= "Sender: $from \r\n";
  178. $head .= "Reply-To: $from \r\n";
  179. $head .= "Organization: $org \r\n";
  180. $head .= "X-Sender: $from \r\n";
  181. $head .= "X-Priority: 3 \r\n";
  182. $head .= "X-Mailer: $mailer \r\n";
  183. $body = str_replace("\r\n", "\n", $body);
  184. $body = str_replace("\n", "\r\n", $body);
  185. return @mail($to, $title, $body, $head);
  186. }
  187. function getThemes() {
  188. $themes_dir = "themes";
  189. $dirs = array();
  190. $path = getcwd();
  191. $dir = (substr(PHP_OS, 0, 3) == 'WIN') ? $path."\\".$themes_dir : $path."/".$themes_dir;
  192. $dir = str_replace("admin\\", "", $dir);
  193. $dir = str_replace("admin/", "", $dir);
  194. $handle = opendir($dir);
  195. $i=0;
  196. while($filename = readdir($handle)) {
  197. if($filename != "." && $filename != "..") {
  198. $dirs[$i]=trim($filename);
  199. $i++;
  200. }
  201. }
  202. closedir($handle);
  203. return $dirs;
  204. }
  205. function sanitizeName($name) {
  206. $name = preg_replace('/[\'"]/', '', $name);
  207. $name = preg_replace('/[^a-zA-Z0-9]+/', '-', $name);
  208. $name = trim($name, '-');
  209. $name = strtolower($name);
  210. //HACK: We need to rework the regular expression to allow the dot
  211. $ext = substr($name, strlen($name)-3, strlen($name));
  212. $body = substr($name, 0, strlen($name)-4);
  213. $name = $body.".".$ext;
  214. return $name;
  215. }
  216. function _file_get_contents($path) {
  217. // Modified function from:
  218. // http://work.dokoku.net/Anieto2k/_file_get_contents.phps
  219. // http://www.anieto2k.com/2007/02/09/file_get_contents-y-dreamhost/
  220. if (!preg_match('/^http/i', $path)) {
  221. if ($fp = fopen($path, 'r')) {
  222. return fread($fp, 1024);
  223. } else {
  224. return false;
  225. }
  226. } else {
  227. if (extension_loaded('curl') && version_compare(get_curl_version(), '7.10.5', '>=')) {
  228. $ch = curl_init();
  229. $timeout = 5;
  230. curl_setopt ($ch, CURLOPT_URL, $path);
  231. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  232. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  233. $file_contents = curl_exec($ch);
  234. curl_close($ch);
  235. if (is_string($file_contents)) {
  236. return $file_contents;
  237. } else {
  238. return false;
  239. }
  240. } else {
  241. $data = parse_url($path);
  242. if (!$data['host'] || $data['scheme'] != "http") {
  243. return false;
  244. }
  245. $f = @fsockopen($data['host'], ($data['port']) ? $data['port'] : 80, $e1, $e2, 3);
  246. if (!$f) {
  247. return false;
  248. }
  249. $q = "GET " . $data['path'] . (isset($data['query'])?'?'.$data['query']:'') . " HTTP/1.1\r\n";
  250. $q .= "Host: " . $data['host'] . "\r\n";
  251. $q .= "Connection: close\r\n";
  252. $q .= "Referer: http://www.gelatocms.com/\r\n\r\n";
  253. $recv = "";
  254. fwrite($f, $q);
  255. while (!feof($f)) {
  256. $recv .= fread($f, 1024);
  257. }
  258. $request = $q;
  259. $response = substr($recv, 0, strpos($recv, "\r\n\r\n"));
  260. $body = substr($recv, strpos($recv, "\r\n\r\n") + 4);
  261. if (preg_match('/http\/1\\.[0|1] ([0-9]{3})/i', $response, $res)) {
  262. if ($res[1][0] != "2") {
  263. return false;
  264. }
  265. } else {
  266. return false;
  267. }
  268. if (preg_match('/transfer-encoding:\s*chunked/i', $response)) {
  269. $tmp_body = $body;
  270. $new = "";
  271. $exit = false;
  272. while (!$exit) {
  273. if (preg_match('/^([0-9a-f]+).*?\r\n/i', $tmp_body, $res)) {
  274. $len = hexdec($res[1]);
  275. if ($len == "0") {
  276. $exit = true;
  277. break;
  278. }
  279. $new .= substr($tmp_body, strlen($res[0]), $len);
  280. $tmp_body = substr($tmp_body, strlen($res[0]) + $len + strlen("\r\n"));
  281. } else {
  282. $exit = true;
  283. }
  284. }
  285. $body = $new;
  286. }
  287. return $body;
  288. }
  289. }
  290. }
  291. function get_curl_version() {
  292. $curl = 0;
  293. if (is_array(curl_version())) {
  294. $curl = curl_version();
  295. $curl = $curl['version'];
  296. } else {
  297. $curl = curl_version();
  298. $curl = explode(' ', $curl);
  299. $curl = explode('/', $curl[0]);
  300. $curl = $curl[1];
  301. }
  302. return $curl;
  303. }
  304. function transform_offset($offset){
  305. $sp = strpos($offset , ".")? explode("." , $offset) : false;
  306. if(is_array($sp)){
  307. $minutes = strval($sp[1]);
  308. $off_h = $sp[0]*3600;
  309. $off_m = (($minutes*60)/100)*60;
  310. $off = $off_h+$off_m;
  311. } else {
  312. $off = ($offset*3600);
  313. }
  314. return $off;
  315. }
  316. function getLangs() {
  317. $langs_dir = "languages";
  318. $dirs = array();
  319. $path = getcwd();
  320. $dir = (substr(PHP_OS, 0, 3) == 'WIN') ? $path."\\".$langs_dir : $path."/".$langs_dir;
  321. $dir = str_replace("admin\\", "", $dir);
  322. $dir = str_replace("admin/", "", $dir);
  323. $i=0;
  324. $cls_lang_dir = @ dir($dir);
  325. while (($directory = $cls_lang_dir->read()) !== false) {
  326. if($directory != "." && $directory != "..") {
  327. $dir2 = (substr(PHP_OS, 0, 3) == 'WIN') ? $path."\\".$langs_dir."\\".$directory : $path."/".$langs_dir."/".$directory;
  328. $dir2 = str_replace("admin\\", "", $dir2);
  329. $dir2 = str_replace("admin/", "", $dir2);
  330. if(is_dir($dir2)){
  331. $cls_lang_dir2 = @ dir($dir2);
  332. while (($directory2 = $cls_lang_dir2->read()) !== false) {
  333. if($directory2 != "." && $directory2 != "..") {
  334. if (preg_match('|^\.+$|', $directory2)){
  335. continue;
  336. }
  337. if (preg_match('|\.mo$|', $directory2)){
  338. if(!in_array($directory2,$dirs)){
  339. $dirs[$i]=trim($directory);
  340. $i++;
  341. }
  342. }
  343. }
  344. }
  345. }
  346. }
  347. }
  348. $dirs = array_unique($dirs);
  349. return $dirs;
  350. }
  351. function removeBadTags($source) {
  352. $validTags ='<p><ol><ul><li><a><abbr><acronym><blockquote><code><pre><em><i><strike><s><strong><b><br><span><div><img>';
  353. $source = strip_tags($source, $validTags);
  354. return preg_replace('/<(.*?)>/ie', "'<'.removeBadAtributes('\\1').'>'", $source);
  355. }
  356. function removeBadAtributes($sourceTag) {
  357. $badAtributes = 'javascript:|onclick|ondblclick|onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|onkeydown|onkeyup';
  358. $sourceTag = stripslashes($sourceTag);
  359. $sourceTag = preg_replace("/$badAtributes/i", "niceTry", $sourceTag);
  360. return $sourceTag;
  361. }
  362. function type2Text($number) {
  363. $tmpStr = "";
  364. switch ($number) {
  365. case "1":
  366. $tmpStr = "post";
  367. break;
  368. case "2":
  369. $tmpStr = "photo";
  370. break;
  371. case "3":
  372. $tmpStr = "quote";
  373. break;
  374. case "4":
  375. $tmpStr = "url";
  376. break;
  377. case "5":
  378. $tmpStr = "conversation";
  379. break;
  380. case "6":
  381. $tmpStr = "video";
  382. break;
  383. case "7":
  384. $tmpStr = "mp3";
  385. break;
  386. }
  387. return $tmpStr;
  388. }
  389. function type2Number($string) {
  390. $tmpStr = "";
  391. switch ($string) {
  392. case "post":
  393. $tmpStr = "1";
  394. break;
  395. case "photo":
  396. $tmpStr = "2";
  397. break;
  398. case "quote":
  399. $tmpStr = "3";
  400. break;
  401. case "url":
  402. $tmpStr = "4";
  403. break;
  404. case "conversation":
  405. $tmpStr = "5";
  406. break;
  407. case "video":
  408. $tmpStr = "6";
  409. break;
  410. case "mp3":
  411. $tmpStr = "7";
  412. break;
  413. }
  414. return $tmpStr;
  415. }
  416. ?>