A tumblelog CMS built on AJAX, PHP and MySQL.

functions.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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/") || beginsWith($videoUrl, "http://www.vimeo.com/"))
  106. return true;
  107. else
  108. return false;
  109. }
  110. function getVimeoVideoUrl($videoUrl) {
  111. if(substr_count($videoUrl,"clip:")==1)
  112. return array_pop(explode("clip:",$videoUrl));
  113. else
  114. return array_pop(explode("/",$videoUrl));
  115. }
  116. function isYoutubeVideo($videoUrl) {
  117. $url = explode("?", $videoUrl);
  118. if((beginsWith($url[0], "http://") && endsWith($url[0], ".youtube.com/watch")) || beginsWith($url[0], "http://youtube.com/watch"))
  119. return true;
  120. else
  121. return false;
  122. }
  123. function isYahooVideo($videoUrl){
  124. if (beginsWith($videoUrl, "http://video.yahoo.com/watch/") || beginsWith($videoUrl, "http://www.video.yahoo.com/watch/"))
  125. return true;
  126. else
  127. return false;
  128. }
  129. function getYahooVideoCode($videoUrl){
  130. $params = explode("http://video.yahoo.com/watch/", $videoUrl);
  131. $params2 = explode("/",$params[1]);
  132. $values[0] = $params2[0];
  133. $values[1] = $params2[1];
  134. return $values;
  135. }
  136. function getYoutubeVideoUrl($videoUrl) {
  137. $params = explode("?v=", $videoUrl);
  138. $params2 = explode("&",$params[1]);
  139. return $params2[0];
  140. }
  141. function isDailymotionVideo($videoUrl) {
  142. if (beginsWith($videoUrl, "http://www.dailymotion.com/video/") || beginsWith($videoUrl, "http://dailymotion.com/video/"))
  143. return true;
  144. else
  145. return false;
  146. }
  147. function getDailymotionVideoUrl($videoUrl) {
  148. $params = explode("video/", $videoUrl);
  149. $params2 = explode("_",$params[1]);
  150. return $params2[0];
  151. }
  152. function isSlideSharePresentation($videoUrl) {
  153. if (beginsWith($videoUrl, "[slideshare id="))
  154. return true;
  155. else
  156. return false;
  157. }
  158. function getSlideSharePresentationCode($videoUrl) {
  159. $videoUrl = str_replace("[slideshare id=", "", $videoUrl);
  160. $videoUrl = str_replace("&doc=", " ", $videoUrl);
  161. $videoUrl = str_replace("&w=", " ", $videoUrl);
  162. return explode(" ",$videoUrl);
  163. }
  164. function isVideo($url) {
  165. if (isYoutubeVideo($url)) { return true; }
  166. elseif (isVimeoVideo($url)) { return true; }
  167. elseif (isDailymotionVideo($url)) { return true; }
  168. elseif (isYahooVideo($url)) { return true; }
  169. elseif (isSlideSharePresentation($url)) { return true; }
  170. else { return false; }
  171. }
  172. function sendMail($to, $title, $body, $from) {
  173. $rp = trim($from);
  174. $org = "gelato CMS";
  175. $mailer = "gelato CMS Mailer";
  176. $head = '';
  177. $head .= "Content-Type: text/html \r\n";
  178. $head .= "Date: ". date('r'). " \r\n";
  179. $head .= "Return-Path: $rp \r\n";
  180. $head .= "From: $from \r\n";
  181. $head .= "Sender: $from \r\n";
  182. $head .= "Reply-To: $from \r\n";
  183. $head .= "Organization: $org \r\n";
  184. $head .= "X-Sender: $from \r\n";
  185. $head .= "X-Priority: 3 \r\n";
  186. $head .= "X-Mailer: $mailer \r\n";
  187. $body = str_replace("\r\n", "\n", $body);
  188. $body = str_replace("\n", "\r\n", $body);
  189. return @mail($to, $title, $body, $head);
  190. }
  191. function getThemes() {
  192. $themes_dir = "themes";
  193. $dirs = array();
  194. $path = getcwd();
  195. $dir = (substr(PHP_OS, 0, 3) == 'WIN') ? $path."\\".$themes_dir : $path."/".$themes_dir;
  196. $dir = str_replace("admin\\", "", $dir);
  197. $dir = str_replace("admin/", "", $dir);
  198. $handle = opendir($dir);
  199. $i=0;
  200. while($filename = readdir($handle)) {
  201. if($filename != "." && $filename != "..") {
  202. $dirs[$i]=trim($filename);
  203. $i++;
  204. }
  205. }
  206. closedir($handle);
  207. return $dirs;
  208. }
  209. function sanitizeName($name) {
  210. $name = preg_replace('/[\'"]/', '', $name);
  211. $name = preg_replace('/[^a-zA-Z0-9]+/', '-', $name);
  212. $name = trim($name, '-');
  213. $name = strtolower($name);
  214. //HACK: We need to rework the regular expression to allow the dot
  215. $ext = substr($name, strlen($name)-3, strlen($name));
  216. $body = substr($name, 0, strlen($name)-4);
  217. $name = $body.".".$ext;
  218. return $name;
  219. }
  220. function _file_get_contents($path) {
  221. // Modified function from:
  222. // http://work.dokoku.net/Anieto2k/_file_get_contents.phps
  223. // http://www.anieto2k.com/2007/02/09/file_get_contents-y-dreamhost/
  224. if (!preg_match('/^http/i', $path)) {
  225. if ($fp = fopen($path, 'r')) {
  226. return fread($fp, 1024);
  227. } else {
  228. return false;
  229. }
  230. } else {
  231. if (extension_loaded('curl') && version_compare(get_curl_version(), '7.10.5', '>=')) {
  232. $ch = curl_init();
  233. $timeout = 5;
  234. curl_setopt ($ch, CURLOPT_URL, $path);
  235. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  236. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  237. $file_contents = curl_exec($ch);
  238. curl_close($ch);
  239. if (is_string($file_contents)) {
  240. return $file_contents;
  241. } else {
  242. return false;
  243. }
  244. } else {
  245. $data = parse_url($path);
  246. if (!$data['host'] || $data['scheme'] != "http") {
  247. return false;
  248. }
  249. $f = @fsockopen($data['host'], ($data['port']) ? $data['port'] : 80, $e1, $e2, 3);
  250. if (!$f) {
  251. return false;
  252. }
  253. $q = "GET " . $data['path'] . (isset($data['query'])?'?'.$data['query']:'') . " HTTP/1.1\r\n";
  254. $q .= "Host: " . $data['host'] . "\r\n";
  255. $q .= "Connection: close\r\n";
  256. $q .= "Referer: http://www.gelatocms.com/\r\n\r\n";
  257. $recv = "";
  258. fwrite($f, $q);
  259. while (!feof($f)) {
  260. $recv .= fread($f, 1024);
  261. }
  262. $request = $q;
  263. $response = substr($recv, 0, strpos($recv, "\r\n\r\n"));
  264. $body = substr($recv, strpos($recv, "\r\n\r\n") + 4);
  265. if (preg_match('/http\/1\\.[0|1] ([0-9]{3})/i', $response, $res)) {
  266. if ($res[1][0] != "2") {
  267. return false;
  268. }
  269. } else {
  270. return false;
  271. }
  272. if (preg_match('/transfer-encoding:\s*chunked/i', $response)) {
  273. $tmp_body = $body;
  274. $new = "";
  275. $exit = false;
  276. while (!$exit) {
  277. if (preg_match('/^([0-9a-f]+).*?\r\n/i', $tmp_body, $res)) {
  278. $len = hexdec($res[1]);
  279. if ($len == "0") {
  280. $exit = true;
  281. break;
  282. }
  283. $new .= substr($tmp_body, strlen($res[0]), $len);
  284. $tmp_body = substr($tmp_body, strlen($res[0]) + $len + strlen("\r\n"));
  285. } else {
  286. $exit = true;
  287. }
  288. }
  289. $body = $new;
  290. }
  291. return $body;
  292. }
  293. }
  294. }
  295. function get_curl_version() {
  296. $curl = 0;
  297. if (is_array(curl_version())) {
  298. $curl = curl_version();
  299. $curl = $curl['version'];
  300. } else {
  301. $curl = curl_version();
  302. $curl = explode(' ', $curl);
  303. $curl = explode('/', $curl[0]);
  304. $curl = $curl[1];
  305. }
  306. return $curl;
  307. }
  308. function transform_offset($offset){
  309. $sp = strpos($offset , ".")? explode("." , $offset) : false;
  310. if(is_array($sp)){
  311. $minutes = strval($sp[1]);
  312. $off_h = $sp[0]*3600;
  313. $off_m = (($minutes*60)/100)*60;
  314. $off = $off_h+$off_m;
  315. } else {
  316. $off = ($offset*3600);
  317. }
  318. return $off;
  319. }
  320. function getLangs() {
  321. $langs_dir = "languages";
  322. $dirs = array();
  323. $path = getcwd();
  324. $dir = (substr(PHP_OS, 0, 3) == 'WIN') ? $path."\\".$langs_dir : $path."/".$langs_dir;
  325. $dir = str_replace("admin\\", "", $dir);
  326. $dir = str_replace("admin/", "", $dir);
  327. $i=0;
  328. $cls_lang_dir = @ dir($dir);
  329. while (($directory = $cls_lang_dir->read()) !== false) {
  330. if($directory != "." && $directory != "..") {
  331. $dir2 = (substr(PHP_OS, 0, 3) == 'WIN') ? $path."\\".$langs_dir."\\".$directory : $path."/".$langs_dir."/".$directory;
  332. $dir2 = str_replace("admin\\", "", $dir2);
  333. $dir2 = str_replace("admin/", "", $dir2);
  334. if(is_dir($dir2)){
  335. $cls_lang_dir2 = @ dir($dir2);
  336. while (($directory2 = $cls_lang_dir2->read()) !== false) {
  337. if($directory2 != "." && $directory2 != "..") {
  338. if (preg_match('|^\.+$|', $directory2)){
  339. continue;
  340. }
  341. if (preg_match('|\.mo$|', $directory2)){
  342. if(!in_array($directory2,$dirs)){
  343. $dirs[$i]=trim($directory);
  344. $i++;
  345. }
  346. }
  347. }
  348. }
  349. }
  350. }
  351. }
  352. $dirs = array_unique($dirs);
  353. return $dirs;
  354. }
  355. function removeBadTags($source,$secure=false) {
  356. if($secure){
  357. $validTags ='<abbr><acronym><em><i><strong><b><span>';
  358. } else {
  359. $validTags ='<p><ol><ul><li><a><abbr><acronym><blockquote><code><pre><em><i><strong><b><del><br><span><div><img>';
  360. }
  361. $source = strip_tags($source, $validTags);
  362. return preg_replace('/<(.*?)>/ie', "'<'.removeBadAtributes('\\1').'>'", $source);
  363. }
  364. function removeBadAtributes($sourceTag) {
  365. $badAtributes = 'javascript:|onclick|ondblclick|onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|onkeydown|onkeyup';
  366. $sourceTag = stripslashes($sourceTag);
  367. $sourceTag = preg_replace("/$badAtributes/i", "niceTry", $sourceTag);
  368. return $sourceTag;
  369. }
  370. function type2Text($number) {
  371. $tmpStr = "";
  372. switch ($number) {
  373. case "1":
  374. $tmpStr = "post";
  375. break;
  376. case "2":
  377. $tmpStr = "photo";
  378. break;
  379. case "3":
  380. $tmpStr = "quote";
  381. break;
  382. case "4":
  383. $tmpStr = "url";
  384. break;
  385. case "5":
  386. $tmpStr = "conversation";
  387. break;
  388. case "6":
  389. $tmpStr = "video";
  390. break;
  391. case "7":
  392. $tmpStr = "mp3";
  393. break;
  394. }
  395. return $tmpStr;
  396. }
  397. function type2Number($string) {
  398. $tmpStr = "";
  399. switch ($string) {
  400. case "post":
  401. $tmpStr = "1";
  402. break;
  403. case "photo":
  404. $tmpStr = "2";
  405. break;
  406. case "quote":
  407. $tmpStr = "3";
  408. break;
  409. case "url":
  410. $tmpStr = "4";
  411. break;
  412. case "conversation":
  413. $tmpStr = "5";
  414. break;
  415. case "video":
  416. $tmpStr = "6";
  417. break;
  418. case "mp3":
  419. $tmpStr = "7";
  420. break;
  421. }
  422. return $tmpStr;
  423. }
  424. ?>